Saturday 19 March 2016

Setting up a permanent 301 redirect via .htaccess on Linux server

Redirect individual files

To redirect individual files, like example.com/oldurl.htm to newurl.htm you can use a 301 redirect like this:
Redirect 301 /oldurl.htm /newurl.htm
To redirect one specific file to another domain such as example.com/oldurl.htm to example.in/newurl.htm:
Redirect 301 /oldurl.htm http://example.net/newurl.htm

Redirect an old domain to a new domain

If you had an old domain name such as xyz.com, and now you decided you actually want to use new domain xyz.in for the website. You could setup a 301 redirect for the entire domain, so that old links to xyz.com carry over.
Code in the xyz.com domain's .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^xyz.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.xyz.com [NC]
RewriteRule ^(.*)$ http://xyz.in/$1 [L,R=301,NC]

Force to use www. in domain

A search engine like Google,yahoo,bing would see xyz.com and www.xyz.com as essentially two separate websites. They recommend you pick one version
Code in the xyz.com domain's .htaccess file:

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

No comments:

Post a Comment