Forum Moderators: phranque
I have a tool called NIC tool which allows me to manage DNS settings. And I have a website called example.com which is currently accessible trough www.example.com, somesubdomain.example.com, test.example.com. All these (sub)domains point to the same ip address.
Now, I want all these domains to redirect to www.example.com. That is, when I type somesubdomain.example.com it should redirect to www.example.com and that is what the address bar should show.
How would I do this? Should this be done in the Apache VHOST config or can I accomplish this trouhh my DNS management tool?
thanks
If all of these domains and subdomains resolve to the same vHost (i.e. if the ServerAlias specifies a wildcard subdomain), then use mod_rewrite to check %{HTTP_HOST} and 301-redirect to the canonical domain (again retaining the "page") if and only if the requested hostname is NOT exactly the canonical domain (or blank, to prevent an infinite loop in case you get an HTTP/1.0 request without a Host: header).
Example for use in httpd.conf, outside of any <Directory> container:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]