We all knows HTTPS is the use of Secure Socket Layer (SSL) or Transport Layer Security (TLS) as a sublayer under regular HTTP application. The HTTPS protocol encrypts and decrypts user’s page requests as well as the pages that are returned back by the Web server.

In this little tutorial We will show you how we can redirect our normal http traffic to https using apache mod_rewrite module.

We will implement this howto on a webmail. We want to protect our user’s information such as username & password, lets say we have url http://mail.techbabu.com and we want to redirect it to https://mail.techbabu.com and we also don’t want to bother our client that they should use type https instead of http whenever they want to check their emails

So then we need some special and that is mod_rewrite, This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. You can learn here about mod_rewirte.

You need to configure SSL in Apache web server, A working SSL based Apache should be running. We will not discuses here about how to configure SSL.

Make sure your Apache compiled with (mod_rewirte) module. If not then you need to recompile your Apache with mod_rewrite module support.

Let’s edit httpd.conf file and uncomment this line

LoadModule rewrite_module libexec/apache22/mod_rewrite.so

The above line based on FreeBSD operating system. You need to adjust module path according to your OS.

Now add these lines in htttpd.conf file for redirecting http to https

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Save & exit from file and restart your Apache server

That’s all whenever some access your url e.g http://mail.techabu.com it will automatically redirect to https://mail.techbabu.com