Forum Moderators: phranque
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.
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?
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!
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?aff=%1 [QSA,L]
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
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?
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