Apache: Force HTTPS with .htaccess

June 3, 2010

When HTTPS is required and you are working with an Apache server that parses .htaccess files, here is a handy snippet of code to put in the .htaccess that will cause the server to force every page in that directory and all subdirectories to use HTTPS by sending them to the HTTPS URI if they are not already there.

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