Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

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)

10+ Year Member

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 Sep 24, 2009 (gmt 0)

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

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]


Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month