Dealing with Multiple Domain Names Properly

Though it’s less popular these days, some people desire to have multiple domain names pointing to the same site. For example, say you have three domain names:

www.example.com
www.example.net
www.example.org

The problem is that, especially if you market all three domains, people are free to link to any of these domains. That’s a major duplicate content issue. You must pick a “standard” domain and permanently redirect the other domains to that domain.


Let’s pick www.example.com. This is how to do it with mod_rewrite:

RewriteEngine on
RewriteCond % {HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://wwww.example.com/$1 [R=301,L]

Done! Now everything will get redirected to www.example.com. Let’s analyze the rule in detail.


This is the first time you’re using RewriteCond to place a condition for the rule that follows. In this case, you’re interested in verifying that the site has been accessed through www.example.com. Take another look at the RewriteCond line:

RewriteCond % {HTTP_HOST} !^www\.example\.com

This line specifies a condition that is true when the host name (HTTP_HOST) is not(!)www.example.com. The rewrite rule captures the entire query string of the original URL, as (.*), and passes it to http://www.example.com, doing a 301 redirect to the new location. This way, for example, a query to http://www.example/org?query=string would be 301 redirected to http://www.example.com?query=string.



Your Ad Here


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Recent Articles

Powered by Paras Babbar · Designed by Paras Babbar