Apache: Use .htaccess to redirect http to https

August 25, 2018

Image for Apache: Use .htaccess to redirect http to https from unsplash.com

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