【问题标题】:How to force browsers not to cache destination of redirects如何强制浏览器不缓存重定向目标
【发布时间】:2025-11-25 14:40:02
【问题描述】:

我正在编写一个 PHP 脚本,该脚本通过在 URL 中添加“?random”之类的查询来重定向到我博客上的随机 URL。问题是某些浏览器似乎缓存了目的地,因此“?random”参数总是重定向到同一页面。对于某些浏览器,我设法通过使用代码“307”重定向来防止缓存,但现在我发现 Firefox 57 仍然缓存该链接。

是否有为此目的推荐的重定向代码,或任何其他解决方法?我知道我可以添加一个带有随机标记的虚拟参数,例如“?random&token=...”,但我希望访问者可以从他们自己的网站使用该链接,而我显然无法添加随机标记。

【问题讨论】:

  • 你的代码在哪里?
  • 使用Cache-Control HTTP 标头。
  • @charlotte-dunois Cache-Control 是个好主意,谢谢!
  • 测试过,有效! header( 'Cache-Control: no-cache, must-revalidate' ); header( 'Location: ' . $permalink, true, 307 ); exit;

标签: php redirect caching browser


【解决方案1】:

基于@charlotte-dunois 评论,现在作为问题的完整答案:

header( 'Cache-Control: no-cache, must-revalidate' );
header( 'Location: ' . $permalink, true, 307 );
exit;

【讨论】: