我正在调试一个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缓存,但不清楚浏览器重启,所以他们什么时候会清除?


当前回答

在最新的谷歌Chrome版本79上,可以使用Chrome://net-internals 并从左侧面板选择DNS,然后点击清除主机缓存按钮

其他回答

Chrome 71

要清除永久重定向,转到chrome://settings/clearBrowserData,从那里只清除“缓存的图像和文件”清除重定向。

Chrome 48 - 70

转到chrome://net-internals。在顶部红色状态栏右侧,点击向下箭头▼打开下拉菜单,在“工具”组下,选择“清空缓存”。

在48版中,这是唯一对我有效的清除缓存301的方法。

为了测试目的(避免缓存重定向),人们可以打开NEW PRIVATE WINDOW:点击CTRL+SHIFT+N[如果你使用Mozilla,使用P]

正如其他答案所示,在浏览器中缓存的持续时间可能是无限的。这是极其危险和恼人的。所以不要这么做。至少添加缓存头。在htaccess中,我总是这样做,根本没有缓存:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
  # The E=nocache:1 sets the environment variable nocache to the value of one
  RewriteRule ^/?(.*) https://www.example.org/$1 [L,R=301,E=nocache:1]
</IfModule>


<IfModule mod_headers.c>
  ## Set the response header if the "nocache" environment variable is set
  ## in the RewriteRule above.
  Header always set Cache-Control "no-store, no-cache, must-revalidate" env=nocache

  ## Set Expires too ...
  Header always set Expires "Thu, 01 Jan 1970 00:00:00 GMT" env=nocache
</IfModule>

编辑:

如果过去没有缓存301重定向,则必须从目标重定向回源。例子:

如果你有这个

RewriteRule /my-source /my-target [L,R=301]

你需要把这个

# RewriteRule /my-source /my-target [L,R=301]
RewriteRule /my-target /my-source [L,R=301]

确认! !让用户向受影响的url提交post请求,缓存的重定向将被遗忘。

一个快速的取胜方法是在浏览器控制台输入这个:

fetch (example.com/affected/link,{方法:“post”})不要犹豫 (() => {})

如果您了解受影响的浏览器(特别是在开发过程中),则非常有用。

或者,如果您可以访问之前的301重定向页面,那么您可以将此脚本添加到页面中,无论何时访问它,缓存的301都将被遗忘。

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

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