Forum Moderators: phranque
Here is what I want to achieve:
I want to have it so when someone goes to [subdomain.domain.com...] it rewrites to [domains.com...] which isn't too hard.
Here is where it gets confusing for me....
If someone goes to [subdomain.domain.com...] I want it to rewrite to [domains.com...]
Is this possible?
You can do this with a single rule, but you'll need to add one or two RewriteConds. The first one (commented-out for now) is needed unless you want to have a separate id for the "www" subdomain (separate from the example.com domain).
The second one is needed to prevent an infinite loop once you have rewritten the request to an id.
Options +FollowSymLinks
RewriteEngine on
# RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{QUERY_STRING} !id=[^&]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /$1?id=%1 [L]
Note: Do NOT end-anchor domains in HTTP_HOST patterns. If you do, then the rule will be broken if a port address is appended, e.g. example.com:80 -- a perfectly-valid request could break your rule. If you feel you must end-anchor the domain name for some reason, then use ...example.com(:[0-9]{1,5})?$. However, this should almost never be necessary, since TLDs other than exactly ".com" probably won't resolve to your server, and it's unlikely that your server will respond to ports other that 80.
Jim