Below, we will see on how to write rewrite rules with some examples and common rewrite conditions.

(1) Redirect 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 domain.com to google.com

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ “http\:\/\/www\.google\.com\/” [R=301,L]


(3)  Redirect users to access the site with WWW

example :- redirect domain.com to www.domain.com

add the below in .htaccess file

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

(4)  Redirect page to another page within public_html

example1 :- to redirect home.html to index.php

RewriteEngine on
RewriteRule ^home.html$ index.php

example2 :-  rewrite site  http://domain.com/kb/index.php  to   http://domain.com/blog/index.html

go to kb directory and create a .htaccess file

#cd public_html/kb
#touch .htaccess
#vi .htaccess

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