The best non-www to www Magento htaccess redirect

There are many ways to redirect a non-www Magento to www. You may want to do this for SEO purposes. 

I have tried many ways to redirect Magento to www and several of them broke some of my sites functionality (ie payment returns). 

Here is the best code to add to the top of your .htaccess file to redirect Magento to www.

# Force WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$$1 [R=301,L]

Post Author: Tom Pai