Option 1: Redirecting example.com/dir/anypage.html to www.example.com/dir/anypage.html (same page)
Use this format if you want a request for any URL at example.com to redirect to the same page at www.example.com. (If you want to redirect to the home page of www.example.com, see option 2 below.)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [nocase]
RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]
Option 2: Redirecting example.com/dir/anypage.html to the www.example.com home page
Use this format if you want a request for any URL at example.com to redirect to the home page of www.example.com. (If you want to redirect to the same page at www.example.com, see option 1 above.)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [nocase]
RewriteRule ^(.*) http://www.example.com/ [last,redirect=301]