Wednesday 9 February 2011

Redirect to different domain in .htaccess

If you own several domains pointing to the same page and would like to enforce users to always use the same domain you can use apache mod_rewrite by editing .htaccess file. Let's say you own
  • http://mypage.com
  • http://my-page.com
  • http://www.mypage.com
  • http://www.my-page.com
but you want that everybody uses only the http://mypage.com.
Here is how your .htaccess file could look like:
RewriteEngine On
rewritecond %{http_host} ^my-page\.com [nc,OR]
rewritecond %{http_host} ^www\.my-page\.com [nc,OR]
rewritecond %{http_host} ^www\.mypage\.com [nc]
rewriterule ^(.*)$ http://mypage.com/$1 [r=301,nc]

This may be useful if you want that your page is always associated with the same, unique address.
This mechanism is commonly used by webpage owners who don't want other people to earn on popularity of their pages and still their traffic. They simply buy similar domains (like in our example) and redirect them all to their main domain.

No comments: