Apache – How to Fix HTTP to HTTPS Redirect Not Working on Apache 2.4

apache-2.4httpsmod-rewrite

I have this apache config:

<VirtualHost *:80>
DocumentRoot "/home/example/public_html/"
ServerName www.example.com

<Directory "/home/example/public_html/">
  allow from all

  RewriteEngine on
  RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
</Directory>
</VirtualHost>

The redirect never seems to fire and just serves files from http. I've tried all sorts of combinations, but nothing seems to work.

mod_rewrite is enabled (using LoadModule rewrite_module modules/mod_rewrite.so)

Can anyone help?

Best Answer

This is way overcomplicated. You only need two (or three) directives to redirect everything to HTTPS, e.g.

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    Redirect permanent "/" "https://www.example.com/"
</VirtualHost>

This will redirect both with and without www to the canonical HTTPS site.

Related Topic