Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Domain name redirection via Apache VHOST config or via DNS mgmt tool?

How do I do it?

2:08 pm on Sep 24, 2009 (gmt 0)

Full Member

10+ Year Member

joined:Nov 28, 2003
votes: 0

Hi there,

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?


3:49 pm on Sept 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0

If you have separate vHost containers for each of these domains and subdomains, put a mod_alias "RedirectPermanent" directive into each container, specifying that all requested URL-paths are to be 301-redirected to the canonical domain while retaining the requested URL-path (that is, keep the "page," just change the domain).

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]