The situation is: you have an web application or URL that you would like to force your users (or yourself) to use the secure https protocol rather than the unencrypted http protocol. This is easy to do with Apache and .htaccess.

Create or add to the .htaccess file in the root of the web directory you would like to force redirect for. Add the following lines:

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

This says that if https is off, reload the page at the same location using HTTPS instead.

8 comments
  1. Thanks, worked a treat. I’d tried lots of RewriteCond using server-port but couldn’t get it to work.

  2. Thanks, worked a treat. I’d tried lots of RewriteCond using server-port but couldn’t get it to work.

  3. I found lots of interesting information here. The post was professionally written and I feel like the author has extensive knowledge in the subject. Keep it that way.

  4. I found lots of interesting information here. The post was professionally written and I feel like the author has extensive knowledge in the subject. Keep it that way.

Comments are closed.

You May Also Like

PHP Script for controlling APC PDU via SNMP

This one is neat – you can control those APC PDU Power…

Simple Sysadmin Trick: Using tcpdump To Sniff Web Server Traffic

Sometimes, you just have to look into the raw data to see…

Simple Guide To Signing RPMs with FPM

I’ve been using the excellent fpm (Effing package manager!) tool for automatically…

Convert Windows or DOS Encoded Files to Unix/Linux. (ANSI to UTF-8)

Windows files and Unix files (Redhat, Ubuntu, etc) are encoded in different…