A redirect sends users and search engines to a URL that is not requested by them. This 301 redirect response notifies the search engines and the user that the page has moved permanently from the old URL to the new URL.
Why use 301 .htaccess redirect?
- Redirection prevent from canonicalization problems.
- If user wants to change the name of the website then it’s possible by Redirection methods.
- If your visitors and search engines enter your old links then they can easily get your new content.
- Avoid duplicate content penalties from search engines.
- Improve your ranking in SEO.
Useful Methods of a 301 .htaccess redirect
- Method 1: Redirect Old Domain to New Domain via .htaccess
- Method 2: Redirect Certain File Extensions, for example, HTML to PHP
- Method 3: Redirect website on WWW or non WWW version Domain
- Method 4: Redirect individual files on the same domain
- Method 5: redirect individual files on the Different domain
- Method 6: Redirect Website redirect HTTP to HTTPS
In this article, we are going to overlook on 301 permanently redirect. Using these methods we can direct traffic from existing page to the new page.
Ways to Redirect your Website using 301 Redirect
A permanent 301 redirect tells the search engines that the original URL has now moved or updated to a new URL So that search engine crawlers will recognize your 301 redirect code will index your page or site with new URL which you have updated.
Method 1: Redirect Old Domain to New Domain via .htaccess
When users want to redirect site from old domain to new domain. That time we can redirect safely without breaking all the links to the old domain.
There must be a Proper redirection of all the pages on the old domain to the new domain. It will ensure visitors to the old domain will end up in the right place.
If you had an old domain domainracer.com, and now you decided you actually want to use domainracer.net for the website then write the following code to your .htaccess file.
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainracer.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domainracer.com [NC]
RewriteRule ^(.*)$ http://domainracer.net/$1 [L,R=301,NC]
Visitors those are coming to your site through old domain will redirect to the new domain via. htaccess file.
Method 2: Redirect Certain File Extensions for example .php to .htm
If there is need to redirect all the pages of .php to .htm then by using htaccess redirect file extension you can update all links to use the new extension.
Add the following code to your .htaccess file to redirect file extension
Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]
Benefits of file extension redirect are if your site contains a lot of HTML pages and if there is need to change that extension from .php to .htm then instead of making changes to each and every page you can use 301 redirect file extension.
Method 3: Redirect website on WWW or non WWW version Domain
To avoid duplicate content in search engines you can use either the www or the non-www version of website domain.
If user wants to redirect non www to www domain version then following code must include in the .htaccess file
http://www.domainracer.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
Redirect WWW to non WWW version domain add following code to your .htaccess file
http://domainracer.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
301 Redirect from www to non-www or vice versa is helpful to avoid search engines such as Google indexing two versions of your domain.
Method 4: Redirect individual file on the same domain
This method is used to redirect old page to new page. When new file contains information then the user can use this method to redirect from old file to the new file.
Redirect single file on the same domain
To redirect individual page http://domainracer.com/oldfile.htm to newfile.htm use the following code:
Code:
Redirect 301 /oldfile.htm /newfile.htm
Benefits of redirecting traffic using a single page
- Don’t lose any website traffic
It’s better to redirect to the new page instead of seeing 404 error page. Because Google quickly removes pages from its index which returns an error 404 (page not found).
Even if the page has a high number of backlinks pointing to them.
- Important tool for search engine optimization
help you maintain your existing search engine rankings so you do not lose traffic after the transition to another URL.
So, it is very important to proper redirect a single page on your website.
Method 5: Redirect individual file on the different domain
If the user wants to Redirect single file on a different domain then it’s possible by this method.
Redirect single file on the different domain:
Following code is used to redirect domainracer.com/oldfile.htm to domainracer.net/newfile.htm
Code:
Redirect 301 /oldfile.htm http://domainracer.net/oldfile.htm
Method 6: Redirect Website redirects HTTP to HTTPS
HTTPS used to protect highly confidential online transactions like online banking and online shopping order forms.
To redirect http to https permanent you need to add following code in your .htaccess files.
Code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Above code help you to redirect http to https with htaccess.
How 301 differ from 302
- 301 = if you want permanent redirection (SEO benefits)
- 302 = if you want temporary redirection
If there is already existing .htaccess file:
- Avoid duplication of RewriteEngine On.
- Lines beginning from RewriteCond and RewriteRule should immediately follow the already-existing RewriteEngine On.
Pay attention on above points while you redirecting http to https.
We have tried our best to explain the 301 redirect via .htaccess in details.
Hope, this will definitely help you to redirect your website or single page on other URL.
If you find any problem while reading the article or any suggestion then please let us know so that we can update it to make more benefits of other visitors.