How to find the cause of http to https redirect loop

apache-2.4cloudflarehttpsowncloudredirect

I setup owncloud on a new box recently (Apache/2.4.10 Debian). This was before I got an ssl cert through Let's Encrypt. I've been trying to force http to https, but I always end up with redirect loops. I've googled around and I've tried adding:

Redirect permanent / https://www.example.com

This didn't fix the issue.

My current configuration looks like this:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName www.example.com

        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
        ServerName www.example.com
        DocumentRoot /var/www/owncloud

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This too gives me a redirect loop.

Also, if I keep just the *:80 virtual host and remove the rewrite part, I can access both http and https without any issues. So I've got that going for me.

Requested Additional Info Section

apachectl -S output:

VirtualHost configuration:
*:80                   www.example.com (/etc/apache2/sites-enabled/ssl-owncloud.conf:1)
*:443                  www.example.com (/etc/apache2/sites-enabled/ssl-owncloud.conf:14)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex rewrite-map: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

Best Answer

The reported configurations are fine. Is it possible other virtual host definitions are hiding?

Would you like to make sure with:

sudo apache2ctl -t -DDUMP_VHOSTS

then? A sane output is:

VirtualHost configuration:
*:80                   www.example.org (/etc/apache2/sites-enabled/000-default.conf:1)
*:443                  www.example.org (/etc/apache2/sites-enabled/default-ssl.conf:4)

(where :1 and :4 is the line number where the vhost is defined).