We can set http redirection rules in the apache main configuration file, but in some cases, we don’t have permission to edit the default configuration file (e.g. shared host, cPanel server). In this case, they provide the .htaccess file which is an Apache web server feature to make configuration changes on a per-directory basis. It allows you to take directives that would normally be put in apache’s main configuration files, and put them in a directory-specific configuration file instead. In this tutorial, we’ll discuss how to set permanent redirection rules in the .htaccess file. There are many sorts of http redirection rules:
301 Permanent redirect.
302 Temporary redirect.
303 Redirect.
307 redirect.
The 301 redirect is a permanent redirection and is used to indicate to search engines that the originating URL has permanently moved to a new URL. This is the best method for implementing redirection on a website. It acts as the most efficient and searches engine-friendly method for webpage redirection.
Now we’ll discuss some common permanent 301 redirection rules.
HTTP to HTTPS Redirection.
Most of the domains have a valid SSL certificate, but in most cases, the website does not redirect to https. The given rule is helping you to redirect the website to https from https. Put given below redirection rule in the .htaccess file to redirect to HTTPS from all http requests.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ Error! Hyperlink reference not valid. [L,R=301]
Redirect from old files to new files on the same domain.
Assume you have the website examle.com. If you want to redirect from example.com/oldfile.php to example.com/newfile.php put the given below rule in the .htaccess file.
/oldfile.php /newfile.php
Redirect old domain to new domain.
Assume you have a website and two domains example.org and example.com for a website and currently you’re using example.com and now you decided you want to use example.org for your website. In this case, put the given below rule in the .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.org/$1 [L,R=301,NC]
Redirect Non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ Error! Hyperlink reference not valid. [R=301,L]
Redirect all files with specific extensions to a different extension.
For example, if you want to redirect all .htm file to .php
RewriteEngine On
RewriteCond %{REQUEST_URI} .htm$
RewriteRule ^(.*).htm$ /$1.php [R=301,L]
If you need any further help please do reach our support department.