htaccess : how to redirect non-www url to www

Place the following configuration in your .htaccess file:

RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteCond %{HTTP_HOST} !=localhost RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

This line is placed to make sure that redirection does not occur on your localhost during the development process. You can remove this line if you want to place the .htaccess configuration directly on your production server. Otherwise you can leave it and change “localhost” to any other local web server you are running.

RewriteCond %{HTTP_HOST} != localhost

Redirects from .htaccess file

# permanent redirect (301) Redirect 301 /path new_Url

temporary redirect (302)

Redirect 302 /path new_Url