Guide for Setting 301 Redirect Rules for .htaccess
Moved your website from one domain to other and have no clue how to redirect the traffic to a new domain? Then you are stumbled upon the right place. Here we will show how you can redirect your traffic to a new domain. This can be done by setting permanent 301 Redirect rules in the .htaccess file of your website.
There is a vast range of 301 Redirect rules that can be used .htaccess. In this guide, we will show you most common rules of redirection. These are-
- Redirect individual files on the same domain
- Redirect an old domain to a new domain
- Force www. version of the domain to be used or you can force non www. version of the domain to be used
- Redirect all files with a certain extension
Requirements
- Permission to edit .htaccess
Procedure
- Locate your .htaccess file and open it in any editor of your choice
Redirect individual files
To redirect some specific file to another file on the same domain. Like basezap.com/vps.php to basezap.com/ssd-vps.php
Redirect 301 /vps.php /ssd-vps.php
To redirect some specific file to another file on the different domain. Like basezap.com/vps.php to example.com/ssd-vps.php
Redirect 301 /vps.php http://example.com/ssd-vps.php
Redirect the old domain to the new domain
This rule will redirect entire domain to new domain and other files too. Like basezap.com to example.com and basezap.com/vps.php to example.com/vps.php
These rules will reside inside the .htaccess file of basezap.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^basezap.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.basezap.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
Force www. version of the domain to be used
When you want to force www. for your domain if a non www. domain is accessed. Like basezap.com is accessed then it should redirect to www.basezap.com.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^basezap.com [NC]
RewriteRule ^(.*)$ http://www.basezap.com/$1 [L,R=301,NC]
Force non www. version of the domain to be used
When you want to force non www. for your domain if a www. domain is accessed. Like www.basezap.com is accessed then it should redirect to basezap.com.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.basezap.com [NC]
RewriteRule ^(.*)$ http://basezap.com/$1 [L,R=301,NC]
Redirect all files with certain extension
If you want to redirect all .html files to .php extension. Like basezap.com/vps.html to basezap.com/vps.php
RewriteEngine On
RewriteCond %{REQUEST_URI} .html$
RewriteRule ^(.*).html$ /$1.php[R=301,L]
We have covered common 301 redirection rules in this guide.