Redirecting from http to https in Apache

You should configure Apache Virtualhosts to do the job. RewriteMod isn't the appropriate solution for this case and .htaccess isn't either.

In your httpd.conf or equivalent use the following lines accordingly your needs. Edit it to your domain and site.

<VirtualHost *:80>
   ServerName www.example.com example.com
   Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName example.com
   DocumentRoot /usr/local/www/apache2/htdocs
   SSLEngine On

   ** Additional configurations here **

</VirtualHost>

Hope this clarifies the procedure.


On shared hosting, when you don't have better options, you could modify your rewrite rule in the .htaccess:

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

First, the RegEx at the beginning matches to all requests, including everything behind the domain.

Then, a HTTP Result code of 301 (moved permanently) is thrown back to the client together with the new URL. Most of the modern browsers remember the new URL, in this case the httpS, and redirect to the new url automatically the next time the user calls up the website.

I hope this helps, kind regards