URL redirect / rewrite using .htaccess file
by Dinesh[ Edit ] 2014-02-04 19:33:22
URL redirect / rewrite using .htaccess file
Create a 301 redirect forcing all http requests to use either www.example.com or example.com:
Example 1 - Redirect example.com to www.example.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Example 2 - Redirect www.example.com to example.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/?$ "http://example.com/" [R=301,L]
Explanation of this .htaccess 301 redirect:
Let's have a look at the example 1 - Redirect example.com to www.example.com. The first line tells apache to start the rewrite module. The next line:
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]