It has been years since I last time used .htaccess file available in Apache. I'm using Ngnix servers for 99.9 of mine work.

But today I had an opportunity to work on Apache environment, which uses .htaccess file.

So in order to rewrite non www to www in Apache I added following rulles to .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

 

For Ngnix please read this article:

http://www.reinisfischer.com/ngnix-www-non-www