Here's how I do it in .htaccess in Linux:
RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If you already have a RewriteEngine in your .htaccess, you can skip that part.
What this does is check if the host contains www. If it doesn't it redirects to www. version. You can find more info here: http://www.stevefortuna.com/redirect-to-www/