Loads of people wants to redirect their sites from non-www domain to www but can’t find the way. So here is the solution to do that using .htaccess file(only apache users)

And the good thing is this is generic code, so you have only copy and paste it to your .htaccess file and all done. Do not need to add your domain name.

Non-www to www redirect

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

www to non-www redirect

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]

 

RewriteRule ^(.*)$ http://%1/$1 [R=301,L]