Many websites purchase variations of a domain name to protect digital brand identity. At the minimum, companies want to own .com, .net, .org and ccTLD versions of their brand name. Logical step is to attach all these domains to the same website. But, wait - then the same pages will be accessible from different URLs - not good. It’s bad for your search (e.g. Google) rank, traffic analysis and for many other reasons, I am lazy to list here.

If you are using Drupal, the main .htaccess file has a commented-out line that fixes this problem. In the demo version, it allows you to redirect all www.example.com traffic to example.com or vice versa. But following the example, you can easily figure-out how to do it for other cases, as well.

However, there is a problem with the default version. It chops-off the URI part of the URL. Meaning, it will send your request to http://example.com/news/1234 to just http://www.example.com. Not too good.

So, here is the code you need instead:

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