Automatically redirect to HTTPS with .htaccess

I caught the flu last week that’s why I haven’t been posting regularly.  =0(

Anyway, I wrote previously on how to password-protect a website/page, but in this post I’ll show you how to redirect a user automagically to HTTPS.

All you need to do is add the following in the .htaccess file (I’ll be using the Squirrelmail website as an example):

1.  I use Apache 2, so I do:

vi /etc/httpd/conf.d/squirrelmail.conf

Initially, it will look like:

# SquirrelMail is a webmail package written in PHP.
Alias /webmail /usr/share/squirrelmail
<Directory /usr/share/squirrelmail>
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

2.  Add the following, like so:

# SquirrelMail is a webmail package written in PHP.
Alias /webmail /usr/share/squirrelmail
<Directory /usr/share/squirrelmail>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*)
https://%{HTTP_HOST}%{REQUEST_URI}
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

3.  Save and exit by typing :wq!.

4.  Restart Apache like so: service httpd restart.

That’s it!  When someone visits, http://www.website.com/webmail, Apache will automatically redirect the user to https://www.website.com/webmail.

Hope this helps.  Take care!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.