Last Updated: February 25, 2016
·
1.933K
· moak

Force SSL with .htaccess & mod_rewrite

If you are already using mod_rewrite and want to force all traffic to go through SSL you can add following rule into your htaccess file

<IfModule mod_rewrite.c>
    RewriteEngine On
    <IfModule mod_ssl.c>
      RewriteCond %{HTTPS} !=on
      RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
    </IfModule>
    # Your own rewrite rules here
</IfModule>

By putting the rule in the <IfModule mod_ssl.c> tag I can disable mod_ssl on my local server and use http while forcing SSL on my live server which has a SSL certificate