Forum Moderators: phranque

Message Too Old, No Replies

Need some .htaccess help

         

nehpets

5:44 pm on Dec 29, 2009 (gmt 0)

10+ Year Member



Hi all,

I've got a site with wild card DNS (so that any sub domain will resolve) and I've got the site to where all the requests made are being redirected back to the root. I want to append whatever the sub domain value is with an "aff" attribute in the query string. I've had something that works if your going to sub.test.com, but if you come to the site with an existing query string (sub.test.com?foo=bar) it doesn't append the aff=sub. I've been trying to write something that will accomplish this as the aff=### is what triggers a tracking script to credit affiliate sales and I need to be sure its included on every page (all generated via query strings at the moment) if the request is coming from a sub domain.

Here is what I've got so far:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
RewriteCond %{QUERY_STRING} ([^$]+)$
RewriteRule ^(.*)$ index.php?%2&aff=%1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
RewriteRule ^(.*)$ index.php?aff=%1 [L]

Originally I just had the second statement, I've since added the first statement and I was hoping the added condition on there being a query string would let me grab any existing query string and append the aff value. I tried QSA but it didn't seem to be doing what I needed.

Thanks for any help anyone can provide.

jdMorgan

9:29 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the second rule, but add the [QSA] flag to the rule. See RewriteRule documentation for flag info.

Jim

nehpets

6:43 pm on Jan 6, 2010 (gmt 0)

10+ Year Member



Well I gave the second rule with [QSA,L] and just [QSA] but its not doing what I need thus far.

This is the rule I tried:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
RewriteRule ^(.*)$ index.php?aff=%1 [QSA]

What its doing is if I come to the site with say buck.domain.com its correctly rewriting it to domain.com?aff=buck. The problem is if you come to the site with a query string already there it doesn't do the rewrite. So something like buck.domain.com?option=com_content&view=category&layout=blog&id=45&Itemid=93 doesn't do the rewrite, which I would need it to do domain.com?option=com_content&view=category&layout=blog&id=45&Itemid=93&aff=buck.

Any suggestions on how to accomplish this?

nehpets

7:08 pm on Jan 6, 2010 (gmt 0)

10+ Year Member



Nvm, I got it.

I'm using Joomla (As you may have noticed by the querystring) so I just enabled SEF URLs and the removes the querystring makeing my rule function the way I'd like it. I still have some querystrings on my store URLs, but I did the QSA,L flag on that RewriteRule and it seems to be working the way I want.

Thanks for the help!

jdMorgan

7:11 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You also need a loop-stopper, an [L] flag to prevent later rules from changing the result of this one, and a re-arrangement of the RewriteConds to avoid unnecessary (and slow) file-exists checks:

RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?aff=%1 [QSA,L]

From what you wrote above, the problem is likely to be elsewhere, since this code 'doesn't care' whether there's already a query string on the request. But the several minor tweaks above may help or change the problem, and so are worth testing.

If no change is evident, then we'll likely need to examine the location of this code, any other rules preceding this one, and what other configuration code or settings you might have that might be interfering.

Jim

nehpets

8:49 pm on Jan 6, 2010 (gmt 0)

10+ Year Member



Could you explain the first condition please? I'm not sure of what it is doing exactly.

I added the L flag after I made that last post but forgot to edit the post, sorry about that.

I attempted the rule you gave but it resulted in the same behavior of if a query string is present the aff=subdomain is not appending to the existing query string. If no query string is present then the rewrite works fine. Here is the .htaccess up to that rule, please ignore the comments, they will be removed once I'm satisfied with how this file is working.


RewriteEngine On

########## Begin - Strip www
RewriteCond %{REQUEST_URI} !^/robots\.txt$
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
########## End - Strip www

########## Begin - Administration redirect
RewriteCond %{HTTP_HOST} ^([^.]+\.)?domain\.com
RewriteRule ^admin(/.*)?$ http://domain.com/administrator$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com
RewriteRule ^admin(istrator)?(/.*)?$ http://domain.com/administrator$2 [R=301,L]
########## End - Administration redirect

########## Begin - Affiliate redirect
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com
RewriteRule ^affiliate(/.*)?$ http://domain.com/affiliate$1 [R=301,L]
########## End - Affiliate redirect

########## Begin - Subdomain Redirect
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?aff=%1 [QSA,L]
########## End - Subdomain Redirect

I'm trying to optimize the administration redirect, if you know of any way plz let me know ;-)

Also, as an aside, do you know of any script I can call via PHP that will create an email on a domain hosted on cPannel (Godaddy VPS)? Or any site that I can go over that may know how to do that?

jdMorgan

7:37 am on Jan 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That first RewriteCond simply aborts the rule if this request has already been rewritten to /index.php and the rule is being re-executed as a result of the fact that if any rule in .htaccess is invoked, rule processing is re-started. This saves an unnecessary file-exists checks on a file that *must* exist (It is Joomla's main script). There is therefore no need to go read the disk a second time to find out whether that main file exists.

Adding that RewriteCond essentially doubles the execution speed of this rule, and should have no effect whatsoever on your aff codes... I have no idea what is causing that problem, but since the rule has a [QSA] flag, the problem is not in this code here... Do be sure to flush your browser cache after changing any code, BTW.

I am not sure about your 'subdomains' situation but in general, your currently-first rule (strip www) should be moved to the position after your current fourth rule. Then let the now-first three rules redirect to both the proper directory and the proper canonical domain -- all at once. Otherwise, as it is now, you could easily get stacked multiple redirects for a single user request...

Oh, and don't remove your comments. In fact, I'd suggest making them more specific and as accurate as possible (e.g. your last rule is a rewrite, not a redirect). I used to get rid of programmers who did not or could not clearly comment code; I did not want them on my projects at all. The parser sees that first "#" and quits parsing the line right there, so comments 'cost' almost nothing. I don't suggest using lots of "#" characters as you've done, though. Try to make every character useful.

Jim