Forum Moderators: phranque
I am wondering if anyone could help me with this code to add something that says: do not redirect to the www.domain.com version of the page if the domain is a subdomain ie: test.domain.com
So, I'd like to retain my redirects for
domain.com goes to www.domain.com
but no redirects if it is subdomain.domain.com
Could anyone help me with this please? Thank you!
Here is my generic code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\.]+\.[^\.]+$
RewriteRule ^/(.*)$ [%{HTTP_HOST}...] [R=301,L]
You might want to look around in the other config files, as it's possible that another rule is responsible for the unexpected redirection of your subdomains.
There's also one latent defect in the rule above: It could be unexpected defeated by perfectly-valid, but non-canonical requests for FQDNs or domains with appended port numbers -- or both. Ane example would be: [google.com.:80...] which is perfectly-valid, but non-canonical.
This can be handled properly by using two RewriteConds -- one being a modified version of what you already have:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)\.?(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)(\.(:[0-9]+)?¦:[0-9]+)$
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]
Jim