How to redirect non-www URL's to www using .htaccess

From this article you can learn and how to redirect non-www to www using .htaccess. Ensure that you specify the new host as canonical in all page links or sitemaps. Use 301 redirects to route traffic from the previous host to the new host.