Apache: Use .htaccess to redirect http to https

August 25th, 2018

Warning: This post is 6 years old. Some of this information may be out of date.

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://%{SERVER_NAME}/$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