RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Alternate method is
ServerName www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /home/username/public_html
ServerName example.com
RedirectMatch permanent ^/(.*) http://www.example.com/$1
For more details refer http://www.seobook.com/archives/001714.shtml