Redirection Using .htaccess file Print

  • htaccess redirection
  • 2

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

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


Was this answer helpful?

« Back