如何强制Chrome不缓存重定向?

the*_*edk 7 redirect caching google-chrome

一个简单的HTML代码:

<img src="http://someaddr/image.php">
Run Code Online (Sandbox Code Playgroud)

image.php是一个脚本,它将随机重定向返回到具有所有必需的无缓存标头的静态图像:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Location: http://someaddr/random_image_12345.jpg
Run Code Online (Sandbox Code Playgroud)

问题:当向后导航到此HTML页面时,Chrome(最新的win/mac)不会重新验证地址http://someaddr/image.php.

我曾尝试使用重定向302和303(在RFC中有更强的要求,它应该永远不会被浏览器缓存).这就像IE,FireFox,Opera中的魅力一样.他们总是刷新http://someaddr/image.php.但Chrome没有.

我甚至在Chrome中使用过开发者工具,似乎在网络日志中它甚至没有显示任何获取(缓存或未缓存)的尝试http://someaddr/image.php.网络日志仅显示一个连接http://someaddr/random_image_12345.jpg(缓存).为什么会这么破碎......

我知道在图像源中放置查询字符串的简单/简单的解决方案:

    <img src="http://someaddr/image.php?refresh={any random number or timestamp}">
Run Code Online (Sandbox Code Playgroud)

但我不喜欢/不能使用这样的黑客.还有其他选择吗?

JxA*_*IxN 5

尝试 307 重定向

但是,如果您在尝试访问由于缓存重定向而无法工作的链接时遇到困难...

这不会清除缓存,但如果您正在尝试访问已重定向缓存的链接,这是一种快速且可行的方法。

将链接地址复制到地址栏中,并在地址中添加一些GET信息。

示例 如果您的站点是http://example.com

Put a ?x=y at the end of it 
( example.com?x=y ) - the x and y could be anything you want.
Run Code Online (Sandbox Code Playgroud)

如果已经有 ? 在 url 后面有一些信息

( example.com?this=that&true=t ) - try to add &x=y to the end of it...

( example.com?this=that&true=t&x=y )
Run Code Online (Sandbox Code Playgroud)


awm*_*awm -3

来自 另一个问题中发布的链接:

 The first header Cache-Control: must-revalidate means that browser must send validation request every time even if there is already cache entry exists for this object.
Browser receives the content and stores it in the cache along with the last modified value.
Next time browser will send additional header:
If-Modified-Since: 15 Sep 2008 17:43:00 GMT

This header means that browser has cache entry that was last changed 17:43.
Then server will compare the time with last modified time of actual content and if it was changed server will send the whole updated object along with new Last-Modified value.

If there were no changes since the previous request then there will be short empty-body answer:
HTTP/1.x 304 Not Modified
Run Code Online (Sandbox Code Playgroud)

您可以使用 HTTP 的 etag 和上次修改日期来确保您不会发送已缓存的浏览器数据。

$last_modified_time = filemtime($file); 
$etag = md5_file($file); 

header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); 
header("Etag: $etag"); 

if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || 
    trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { 
    header("HTTP/1.1 304 Not Modified"); 
    exit; 
} 
Run Code Online (Sandbox Code Playgroud)