Saturday, April 11, 2009

Redirect HTTP to HTTPS on Apache

At times we need to run a website over HTTPS only and to retain the traffic coming to the website over HTTP, that needs to be redirected towards HTTPS. Though there are many ways to do so but this method looks easiest to me-

<VirtualHost *:80>
    ServerName mydomain.com

    RedirectPermanent / https://mydomain.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName mydomain.com

    # other SSL configuration goes here
</VirtualHost>


If only parts of the website (say /secure) needs to be HTTPS enabled then redirection can be done something like-

        RedirectPermanent /secure https://mydomain.com/secure

4 Comments:

Raghu said...

Thanks! This was the exact info I was looking for.

M K Athyala said...

Thanks, this post was useful.

Anonymous said...

Thanks. I spent a couple of hours trying to find the correct mod_rewrite syntax (with no success), but your method worked very with very little fuss.

Buliva said...

I think yours is the most easiest of methods. Thanks! I also spent 2 hours searching but yours is just the easiest way out.