How to set up permanent 301 redirect via .htaccess?

A permanent 301 redirect in your .htaccess file lets search engines and others know that an old link has been replaced by a new one. It's the recommended method for directing traffic from an existing page.

Some common uses of a 301 .htaccess redirect:

Redirect individual files on the same domain

An old file has moved locations, or the information is now contained in a new file.

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

Redirect 301 /oldfile.html /newfile.html

To redirect one specific file to another domain such as yourwebsite.com/oldfile.html to yourwebsite.net/newfile.html:

Redirect 301 /oldfile.html http://yourwebsite.net/newfile.html
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.

Example:
If you had an old domain such as yourwebsite.com, and now you decided you actually want to use yourwebsite.net for the website. You could setup a 301 redirect for the entire domain, so that old links to yourwebsite.com carry over.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yourwebsite.com [NC]
RewriteRule ^(.*)$ http://yourwebsite.net/$1 [L,R=301,NC]
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 yourwebsite.com, and www.yourwebsite.com and you can set one as the preferred method that your site displays.

Example: Force www. version of domain to be used
A search engine like Google would see yourwebsite.com and www.yourwebsite.com as essentially two separate websites. They recommend you pick one version you'd like search engines to display and using a 301 redirect is a possible option.

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

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

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

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.yourwebsite.com [NC]
RewriteRule ^(.*)$ http://yourwebsite.com/$1 [L,R=301,NC]
Redirect all files with certain extension

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

Example:
To re-direct all of one type of file to another, such as yourwebsite.com/file.php to yourwebsite.com/file.html

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

You should now know how to properly setup 301 permanent redirects on your website to help ensure that search engines and visitors coming to your site from older links can still get to your new content.

You have the ability to setup redirects for a domain in your cPanel interface one link at a time. You can also add the redirects manually by modifying your .htaccess file directly.

Comments

Popular posts from this blog

About 12 Alwars

Post thumbnails from external URLs don’t appear in Blogger. How to resolve?

What is the unicode character for the close symbol?

ஸ்தல விசேஷம் - திருக்கோவிலூர் திவ்யதேசம்