Use the code below to redirect non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This will redirect http://domain.com to http://www.domain.com.
What are the benefits of redirecting a website:
- Avoid duplicate content in Google and other Search Engines.
- Avoid split page rank for inblound links.
- A more consistent URL.
Redirecting from www to non-www using htacess
Use the code below if you want to do the opposite:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
Be sure to replace domain.com with your own domain.
That's it.