How to redirect non-www to www with .htaccess

If your website is accessible using both www.domain.com and domain.com, it can have a negative effect on any SEO campaigns that are currently active on your website.

In order to prevent duplicate listings on Google, you can redirect all non-www traffic to www, using the .htaccess file.

Simply fire up your favourite text editor, we recommend NotePad++, and edit the .htaccess file. Enter the following 3 lines, replacing all instances of yourdomain.com with your actual domain name.

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

That’s it, now your domain will redirect to the www version for all visitors.