Redirection Using .htaccess file

Here are some simplest rules to set some redirections using the .htaccess file.

 

How to write rewrite rule (URL rewriting, mod_rewrite)

1. To redirect a site from http to https :

Add the below in .htaccess file in public_html

===================================================

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

===================================================

 

2. Redirecting a domain to another domain via .htaccess

Example : Redirect abc.com to google.com

===================================================

RewriteEngine on

RewriteCond %{HTTP_HOST} ^abc\.com$ [OR]

RewriteCond %{HTTP_HOST} ^www\.abc\.com$

RewriteRule ^/?$ “http\:\/\/www\.google\.com\/” [R=301,L]

===================================================

3. To redirect users to access the site with WWW

example :- redirect abc.com to http://www.abc.com

Add the below in .htaccess file

===================================================

RewriteEngine on

RewriteCond %{HTTP_HOST} ^abc\.com$ [NC]

RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301]

===================================================

 

4. Redirect page to another page within public_html

Example 1:- to redirect design.html to index.php

===================================================

RewriteEngine on

RewriteRule ^design.html$ index.php

===================================================

 

Example2 :- rewrite site abc.com/kb/index.php to abc.com/blog/index.html

Go to kb directory and create a .htaccess file using the following commands:

#cd public_html/kb

#touch .htaccess

#vi .htaccess

Then paste the following:

===================================================

RewriteEngine on

RewriteRule ^index.php$ /blog/index.html

===================================================

  •  
  • 150 Users Found This Useful
Was this answer helpful?

Related Articles

How To Install and Use Docker on Ubuntu 16.04

Introduction Docker is an application that makes it simple and easy to run application processes...

Backup and Restore cPanel Accounts via SSH

a. How to create a backup of a cPanel Account via SSH?   1. Log-in to the SSH as the Root...

Change Main IP of the server :: Vesta Panel

If we are changing the main IP of a server installed with Vesta panel, we can use the following...

Change permissions using find command

On a Linux server, if you are in need of changing the permissions of a bulk amount of files or...

Change the time of your vps/dedi

It's very easy, you first need to remove the current time file rm /etc/localtime and replace it...