Search code examples
apachehttp-redirecthttpd.conf

Can't get rid of a persistent Apache2 redirect (even with purge/flush/reinstall)


I'm absolutely stumped here. I was attempting to forward all http requests to https (so port 80 to 443). I tired a few methods, including

Redirect / https:/address.com

Redirect permanent / https://address.com

RedirectRewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

It finally took after I tried Redirect permanent, but the forward only half worked (would redirect to proper host but wouldn't serve the page), but when I tried to undo the redirect, it persisted (so still leading to a broken address).

I tried overriding it, tried flushing DNS, completely purged apache2 and reinstalled, and still this redirect persists. I've run out of ideas. It's not just my browser cache either; I've tested on multiple external IP's with the same result.

The .conf with the broken redirect is as follows (Note no redirect is currently active), this conf is no longer active either.

<VirtualHost *:80>
    ServerName www.example.com

    ServerAdmin webmaster@localhost
    #DocumentRoot /var/www/html

    #LogLevel info ssl:warn

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

    #Redirect / https:/address.com

    #Redirect permanent / https://address.com

    #RedirectRewriteEngine On
    #RewriteCond %{HTTPS} !on
    #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

The only virtual host that's active is the host that I actually want working, which is as follows:

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


<VirtualHost *:443>
    ServerName address.com
    ServerAlias www.address.com
    ServerAlias *.address.com

    ServerAdmin webadmin@localhost
    DocumentRoot /disk2/example_site

    #LogLevel info ssl:warn

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


    SSLEngine on
    SSLCertificateFile example.crt
    SSLCertificateKeyFile example.key
    SSLCACertificatePath /example/
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

And still, the redirect persists.


Solution

  • Never use a permanent redirect.

    As you've found removing it from your server does not remove the redirection for any browser which has visited the page. The instruction is always cached indefinitely by the browser.

    The subsequent rewrite rules mean that you will be supplying an https response to an http request - which is clearly non-sensical.

    Add [L,R] to the final line (not R=301 as suggested elsewhere on stack overflow). And if you really are doing away with non-ssl permanently and completely, use HSTS.

    (BTW to fix the browsers you'll need to interfere with the cache database directly or delete and reinstall)