Reputation: 892
I've got it working in that it redirects the subdomain itself, but it doesn't redirect any of the subdirectories for that subdomain.
For example:
sub.domain.com redirects to www.domain.com
but:
sub.domain.com/directory/ doesn't redirect
Here is my code:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sub\.domain\.com$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com" [R=301,L]
Upvotes: 1
Views: 31
Reputation: 42885
Your pattern in the rewriting rule matches only the root itself, so it does not get applied to anything else. Have a try with this modified version:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com$
RewriteRule ^ http://www.example.com/ [R=301]
This obviously redirects all requests to the root of the www.example.com
host. In case you want to preserve the actual path requested you will need to capture it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com$
RewriteRule ^/?(.*)$ http://www.example.com/$1 [R=301]
That rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT
folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Upvotes: 1