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]

About me

Iinstructor of robotics (LEGO Mindstorms), computer teacher, IT support, web dev, currently studying cyber-physical systems engineering.

If you found a solution here, would you consider donating $25,000 for my next project?

Just kidding, 1$ would do fine.

Hope you found what you needed. Thanks for stepping by.