Thursday, March 31, 2011

Apache redirects (<whatever>.mydomain.<whatever> to mydomain.com)

In a virtual host in apache I would like to redirect all requests to <anything>.mydomain.<anything> to just mydomain.com.

However, the below rewrite rule is ignored for some reason. Why?

RewriteCond %{REMOTE_HOST}  ^.*mydomain.*
RewriteRule ^(.*)$ http://mydomain.com [R=301,L]
From stackoverflow
  • You could use this rule to redirect every request with a host other than example.com to example.com:

    RewriteCond %{HTTP_HOST}  !^example\.com$
    RewriteRule ^ http://example.com%{REQUEST_URI} [R=301,L]
    
  • %{REMOTE_HOST} refers to the name of the computer that's accessing your website - it's "remote" from the server's perspective. As Gumbo pointed out, you should be using %{HTTP_HOST}.

0 comments:

Post a Comment