We mistakenly used a 301 redirect on a very important page, and now that we want to actually use the page, many users' browsers have the 301 stuck in their cache. The problem is that the destination of the redirect is the root of the site, so we can't just blindly 301 redirect it back to break the cache.
I'm trying to use a 3rd URL and an environment variable to force a redirect loop that will cause the browser to check with the server again. The old 301 has been removed, but I'm including it (commented out) for reference.
The problem I'm seemingly running into is that the environment variable I'm attempting to set appears to be completely ignored / never set, so the RewriteCond never allows the rest of the rule to take effect.
RewriteEngine On
#fix /investments 301 redirect:
<IfModule mod_rewrite.c>
RewriteBase /
# This DOES work, I can see TEST_ENV and REDIRECT_TEST_ENV set in the SERVER variable:
# RewriteRule .* - [E=TEST_ENV:TEST,NE]
# The original rule that got us into trouble:
# RewriteRule ^investments\/?$ / [R=301,L]
# Just to be safe, set env variable again at the /investments/ route:
RewriteRule ^investments\/?$ - [E=INVESTMENTSFIX:1]
# 3rd URL is all-investments, and I want to redirect to the problem URL, where it will then go into a redirect loop
RewriteRule ^all-investments/?$ /investments/ [R=301,NC,E=INVESTMENTSFIX:1,L]
# If you land on the homepage with the env variable set, set headers to not cache, redirect back to /investments, which will cause a loop. The browser should then check the server and find that the 301 for /investments no longer applies
<If "%{ENV:INVESTMENTSFIX} == 1">
Header set Cache-Control "no-cache, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
RewriteRule ^/?$ /investments/ [R=301,NC,L]
</If>
# fix homepage once the other rules work??:
# RewriteCond %{ENV:REDIRECT_INVESTMENTSFIX} !1
# RewriteRule ^/$ / [R=301,NC,L]
</IfModule>
Localhost server setup:
- XAMPP
- Apache/2.4.56 (Win64)
- PHP/8.1.17
- OpenSSL/1.1.1t
If an environment variable isn't the way to go, what is a better solution for this? We would really like to salvage this URL, as it's also the parent route for a majority of the website. We didn't realize a 301 redirect was quite THAT permanent (meaning the browser doesn't validate the redirect ever again).