Wednesday, March 27, 2013

How to write rewrite rule (URL rewriting, mod_rewrite)

(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 to

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

(3)  Redirect users to access the site with WWW

example :- redirect to

add the below in .htaccess file

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

(4)  Redirect page to another page within public_html

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

RewriteEngine on
RewriteRule ^home.html$ index.php

example2 :-   rewrite site  to

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

No comments:

Post a Comment