I am not sure how to do this via .htaccess, but I do it with the PHP code in my config.php , which is loaded for each file.
if(substr($_SERVER['SERVER_NAME'],0,4) != "www." && $_SERVER['SERVER_NAME'] != 'localhost') header('Location: http://www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
EDIT: @genesis, you're right, I forgot about https
Edit
header('Location: http://www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
to
header('Location: '. (@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://'). 'www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
Kokos source share