Redirect Loop with Forcing HTTPS Before Basic Authentication
<p>I am attempting, as many people on the internet have done successfully, to force HTTPS on my website, but <strong>before</strong> the Basic Authentication dialogue window appears. I have attempted the two main ways of doing this, including the "FilesMatch" solution and the "ErrorDocument" solution. The former resulted in a Internal Server Error and the latter caused a redirect loop issue. Here is what I have so far (trying to get the Error Document solution to work):</p>

<pre><code>RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

SSLRequireSSL 
ErrorDocument 403 https://website.com/private/https.php

AuthType Basic
AuthName "Password Protected Area"
AuthUserFile /srv/users/serverpilot/apps/intranet/public/.htpasswd
Require valid-user
</code></pre>

<p>The https.php file is simply:</p>

<pre><code>&lt;?php
header("LOCATION: /");
exit();
</code></pre>

<p>My server is running Apache 2.2 and the redirect works after you've authenticated. What am I doing wrong?</p>