You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

利用环境变量与重定向循环强制清除301重定向缓存遇阻

问题描述

我们之前误将重要页面/investments设置为301重定向至网站首页,如今需要恢复该页面正常访问,但大量用户的浏览器已缓存这条301规则。由于重定向目标是首页,无法直接通过反向301来打破缓存。我们尝试借助第三方URL与环境变量触发重定向循环,促使浏览器重新向服务器校验资源状态,目前旧的301规则已移除,但设置的环境变量始终未生效,导致后续Rewrite规则无法触发。

Apache重写配置代码

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>

本地服务器环境

  • XAMPP Apache/2.4.56 (Win64)
  • PHP/8.1.17
  • OpenSSL/1.1.1t

内容的提问来源于stack exchange,提问作者Sean Kendle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 19:03:31