How to Setup permanent 301 redirection via .htaccess

In this tutorial i will explain you to create a .htaccess file to redirect your website invdividual pages to another pages or redirect your whole website to another domain with 301 permanently moved.
It is very important tool to obtain a seo frindly url and redirect the old url to new url.

Some of few uses of 301 htaccess redirection

1. Redirect individual files on the same domain. An old file has moved locations, or the information is now contained in a new file.

2. Redirect an old domain to a new domain . You’ve moved a website from an old domain to a new one, and you want any old links to go to the new site.

3. Force www. version of domain to be used or you can force non www. version of domain to be used Visitors access and link to your website in multiple ways such as domain.com, and www.domain.com and you can set one as the preferred method that your site displays.

Redirect all files with certain extension
You used to have all of your files using an extension like .php and have converted everything to .htm so using a 301 redirect you can update all links to use the new extension.

Here few step you have follow to create file and writing code…

1. Create a new file using a text editor such as notepad, and save it as htaccess.txt.
2. Write your code as your need…….

i. To Redirect individual files

To redirect individual files, like domain.com/oldfile.html to newfile.html you can use a 301 redirect like this:
Redirect 301 /oldfile.html /newfile.html

Note: Here we have to check that the Rewrite engine is on or not. If on then write the code like above and if not then you have firstly start the rewrite engine.
Code will be like this:

RewriteEngine on
Redirect 301 /oldfile.html /newfile.html

ii. To Redirect an old domain to a new domain

To Redirect your whole website to new domain then we have to the code like this bellow. Don’t forget to replace your domain with Name domain. It is necessary when you moved to the new domain.

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

iii. Redirect all files/pages with other extension

To re-direct all of one type of file to another, such as domain.com/file.php to domain.com/file.htm

RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L

iv. Redirect Non www to with www version of domain

Search engine will crawl website with www and without www so if search engine index your both pages then it create content duplicacy so there must need to have one domain with www or without www. here we redirect 301 to one whoes domain we prefer.

If you have a lot of links on the web where people are linking to your site as domain.com, but you would like your visitors to instead end up at www.domain.com you can force this version of your domain with these rules:

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

v. Redirect with www to non www version of domain

Search engine will crawl website with www and without www so if search engine index your both pages then it create content duplicacy so there must need to have one domain with www or without www. here we redirect 301 to one whoes domain we prefer.

If you have a lot of links on the web where people are linking to your site as www.domain.com, but you would like your visitors to instead end up at domain.com you can force this version of your domain with these rules:

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

Vijay maurya

Vijay maurya is a web developer having several years experience and passionate about programming. Vijay worked in many project along with reputed companies. The main aim of blogging is to spread knowledge who need help in development and also to improve knowledge and skills.

Leave a Reply

Your email address will not be published. Required fields are marked *