.htaccess

If you are planning to redirect your site page or main site to some other urls, 301 redirect method is the choice as it is easy to implement and is very search engine friendly. I am penning down few methods for setting up 301 redirect on linux servers using .htaccess. The pre-condition here is that the Apache Mod-Rewrite module should be enabled.

Redirect to www (htaccess redirect)

This method is applicable if you want all requests coming in to domain.com should get redirected to http://www.domain.com. For example if the site http://www.sakshay.in is being accessible by both http://sakshay.in and http://www.sakshay.in, the search engine will see this as two separate urls and may mark one as duplicate content. So this method shall redirect the url http://sakshay.in and all pages to http://www.sakshay.in.

The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domainname.com [nc]
rewriterule ^(.*)$ http://www.domainname.com/$1 [r=301,nc]

Please REPLACE domainname.com and http://www.newdomainname.com with your actual domain name.

Again this method requires Apache Mod-Rewrite module should be enabled on the server for the domain.

 

Redirect Old domain to New domain (htaccess redirect)

This method is applicable if you want that all your directories and pages of your old domain should get correctly redirected to your new domain. Again this method shall prevent from marking the pages as duplicate content by search engines.
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomainname.com/$1 [R=301,L]

Please REPLACE http://www.newdomainname.com in the above code with your actual domain name.

This would take care of the all the old links to be redirected to the new urls. Still it is preferred that in addition to this, your backlinks from other sites be directed to the new urls.

Again this method requires Apache Mod-Rewrite module should be enabled on the server for the domain.