Permanent Redirection Rules in .htaccess File

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.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How can I change a database password on cPanel?

Changing MySQL database password via cPanel You can change your database’s password by...

How can I change my PHP version on cPanel?

Changing PHP version via cPanel Your Hosting Account's PHP version can be changed at any time,...

How can I create a new MySQL database in cPanel?

Creating a new MySQL database at Servers GATE using cPanel   You can easily create a new MySQL...

How can I locate my MySQL database details on cPanel?

Locating your MySQL database details via cPanel You can easily locate your MySQL databases...

How can I set up a redirect on cPanel?

Creating a redirect on cPanel Your hosting redirects can be easily managed with the assistance...