我正在调试一个HTTP 301永久重定向的问题。经过快速测试,Safari似乎在重新启动时清除了它的301缓存,但Firefox却没有。

IE、Chrome、Firefox和Safari什么时候清空它们的301缓存?

For example, if I want to redirect 1.example to 2.example, but I accidentally set it to redirect to 3.example, that is a problem. I can correct the mistake, but anyone who has visited 1.example in the meantime will have cached the incorrect redirect to 3.example, and so they will not be able to reach either 1.example or 2.example until their cache is cleared. Upon investigation, I find that there were no Cache-Control and Expires headers set. The headers for the incorrect 301 response would have been like this:

HTTP/1.1 301 Moved Permanently
Date: Wed, 27 Feb 2013 12:05:53 GMT
Server: Apache/2.2.21 (Unix) DAV/2 PHP/5.3.8
X-Powered-By: PHP/5.3.8
Location: http://3.example/
Content-Type: text/html

我自己的测试表明:

IE7, IE8, Android 2.3.4完全不缓存。 Firefox 18.0.2、Safari 5.1.7(适用于Windows 7)和Opera 12.14都有缓存,并在重新启动浏览器时清除缓存。 IE10和Chrome 25缓存,但不清楚浏览器重启,所以他们什么时候会清除?


当前回答

我有一个简单的解决方案,适用于所有主要的浏览器(最新版本),包括IE, Chrome和FF

Ctrl + Shift + Del - Chrome浏览器:选择“浏览历史记录”和“缓存…” IE:我离开默认选项“临时互联网文件和网站文件”,“Cookies和网站数据”,“历史” FF:“浏览和下载历史”,“缓存” 点击“删除” 关闭并重新打开浏览器。应该可以

其他回答

301是每个HTTP RFC的可缓存响应,浏览器将根据响应上的HTTP缓存头缓存它。使用FireBug或Charles检查响应头,以了解响应将被缓存的确切时间。

如果你想控制缓存持续时间,你可以使用HTTP响应头Cache-Control和Expires来做同样的事情。或者,如果根本不想缓存301响应,可以使用下面的头文件。

Cache-Control: no-store, no-cache, must-revalidate
Expires: Thu, 01 Jan 1970 00:00:00 GMT

在你的。htaccess文件中试试这个:

  <IfModule mod_expires.c>
  ExpiresActive On
  Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
  Header Set Cache-Control "max-age=0, no-store"
  Header Set Cache-Control "no-store"
  Header set Pragma "no-cache"
  </IfModule>

为了解决localhost地址的问题,我更改了站点运行的端口号。这适用于Chrome版本73.0.3683.86。

让用户在该url上提交一个post表单,缓存的重定向消失了:)

<body onload="document.forms[0].submit()">
<form action="https://forum.pirati.cz/unreadposts.html" method="post">
    <input type="submit" value="fix" />
</form>
</body>

作为@thomasrutter的回答

如果您之前发出了301重定向,但想要取消该操作

如果人们在浏览器中仍然有缓存的301重定向,他们将继续被带到目标页面,而不管源页面是否仍然有重定向。解决这个问题的方法包括:

最简单和最好的解决方案是再次发出301重定向。

浏览器会意识到它被引导回到它之前认为是退役的URL,这应该会导致它重新获取该URL,以确认旧的重定向不存在。

如果您无法控制前一个重定向目标所访问的站点,那么您就不走运了。试着请求网站所有者重定向回你。

事实上,这意味着:

a. 301到b.example 删除a.example的301 将b.example 301添加到a.example中

这样就有用了。