This is a great method to get your website indexed correctly by Search Engine Spiders. Many search engine spiders consider domain.com and www.domain.com to be separate and get indexed separately. This can create Pagerank and Duplicate Content issues, if your links are spread out between domain.com and www.domain.com your site will show a lower Pagerank value than it really is. You’ll also risk the chance at search engines thinking you have duplicate content, and in search engine optimization terms this is bad thing.
Add the below code into you .Htaccess File to redirect your non-www url to your www url. You need to change yourdomain.com to your websites domain name.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
If you want to redirect your www url to your non-www version then instead use the below code and insert it into your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]