RewriteEngine On
RewriteCond HTTPS !on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L,QSA]
This should do it
via mod_rewrite manual
[EDIT] swapped "www.example.com" to %{SERVER_NAME} so it redirects to same name you've tried to access, but via https...