Redirect to the SSL Version of a Page with mod_rewrite on Apache

December 23, 2008

                        <User> The web interface doesn't work.
                        <Mike> Type "https" instead of "http."
                        <User> Oh yeah, now it works.
                      

A quick exchange like this is not a big deal on its own, but as every sysadmin knows, little things like this can add up and become annoying very quickly (for all parties involved). I find that if I can prevent these little distractions from happening in the first place, everyone is a little happier and more productive.

One way I accomplish this is by not expecting users to remember which sites need to be accessed via SSL and which do not. I just allow them to connect to everything on port 80, then I let mod_rewrite redirect them to the SSL version as needed:

                      <location "/">
                        RewriteEngine On
                        RewriteCond %{HTTPS} off
                        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
                      </location>
                      
blog comments powered by Disqus