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

时间:2012-11-25 17:20:20

标签: google-chrome caching redirect

一个简单的HTML代码:

<img src="http://someaddr/image.php">

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

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Location: http://someaddr/random_image_12345.jpg

问题:当向后导航到此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}">

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

2 个答案:

答案 0 :(得分:4)

尝试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.

如果已经有?在网址中有一些信息后

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

( example.com?this=that&true=t&x=y )

答案 1 :(得分:-3)

来自另一个问题中发布的link

 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

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

$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; 
}