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

13 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.

Anonymous said...

Great trick, thanks a lot!

Anonymous said...

This will not work. If you try this, you’ll enter on a infinite loop with an error message from the server -flooding error- or the navigator -firefox detects the loop after a few-, or maybe you’ll get old waiting for an answer… The problem is pretty clear: You’re redirecting to the same server, so it executes again that redirection, so you’re redirecting to the same server, so it executes again that redirection, so you’re redirecting to the same server, so it executes again that redirection, so you’re redirecting to the same server, so it executes again that redirection, so you’re redirecting to the same server, so it executes again that redirection, so you’re redirecting to the same server, so it exec… ¿Is it clear enough? I hope so.

Anonymous said...

Thanks, just what I needed!

Anonymous said...

It worked perfectly on my cPanel server. I un-commented the domain's include statement in httpd.conf and created a .conf file with the line above. All other traffic besides the /secure goes to http correctly.

Anonymous said...

Thanks.. Exactly what I was looking for.

Anonymous said...

Thanks It saves my time

Anonymous said...

Thanks alote it saved my time..

Mark said...

Thanks! Find your site when lokking for the syntax

hoangphucit said...

thank you very much ...