Apache: Use .htaccess to redirect http to https


Posted by Andrew McCombe on Sat Aug 25, 2018

I’m forever having to lookup how to use Apache’s .htaccess to redirect http to https so here it is.

First, make sure you have Apache’s mod_rewrite module enabled.


    # Redirect non-https requests to https
    RewriteEngine On
    
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 

This will check the request for HTTPS and if it matches will permanently redirect the browser to the https:// url.

ยป curl -I http://www.euperia.com
    HTTP/1.1 301 Moved Permanently
    Date: Sat, 25 Aug 2018 09:25:44 GMT
    Server: Apache
    Location: https://www.euperia.com/
    Cache-Control: max-age=0
    Expires: Sat, 25 Aug 2018 09:25:44 GMT