12 Oct 2007

HTTP to HTTPS via mod_rewrite 

By - Apache, Quick Tip 4 Comments

To redirect a website from http -> https I utilized mod_rewrite. And created a .htaccess file with the following contents.

$ cat .htaccess
Options FollowSymLinks
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*) https://your_server.com/$1

This can be useful if you need to ensure all access to your site, is done via HTTPS.

4 Responses to “HTTP to HTTPS via mod_rewrite”

  1. Mirge says:

    Thank you, this was PERFECT… I tried another popular rewrite, but it wouldn’t work.. this one worked for me.

  2. Nick Wilkens says:

    Thanks! Glad we could help.

  3. Saad says:

    I only want to run some of the pages on SSL and want site to switch back to non SSL when accessed other pages.

    How can I do this?

    e.g. http://your_server.com/cart/* should run on SSL but any other URL should be on http.

    Thanks

  4. admin says:

    If your web server is running Apache, you may want to look at mod_rewrite (http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html).

    The code below is a start:

    RewriteRule ^/(cart/.*) https://your_server.com/$1

Leave a Reply