避免 301 重定向缓存

发布于 2024-12-29 06:48:44 字数 439 浏览 5 评论 0原文

这是使用 301/303/307 重定向的后续问题对于动态短网址,我尝试确定当目标网址频繁更改时实现短网址重定向的最佳方法。

虽然 301 和 307 重定向似乎都以相同的方式执行,但我关心的问题是 301 重定向缓存(如文档所述 这里)-是避免这种情况的最佳方法,使用 307 重定向代替(我假设307 重定向永远不会缓存?),或者显式发送无缓存标头(“Cache-Control:无缓存,必须重新验证”)?

This is a follow up question to Using 301/303/307 redirects for dynamic short urls, where I try to determine the best method for implementing short url redirection when the destination url will change on a frequent basis.

While it seems that 301 and 307 redirects both perform the same way, the issue that concerns me is 301 redirect caching (as documented here)- is the best way to avoid this to use 307 redirects instead (I'm assuming 307 redirects will never cache?), or to explicitly send a no-cache header ("Cache-Control: no-cache, must-revalidate")?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫言歌 2025-01-05 06:48:44

不要试图避免 301 缓存。如果您不希望任何用户代理缓存您的重定向,那么就不要使用 301 重定向。换句话说,301 缓存将继续存在,从语义上讲,它是一个永久重定向,因此,如果您计划更改目标 URL,301 不是正确使用的状态代码。另一方面,默认情况下不缓存 307 响应

Don't try to avoid 301 caching. If you don't want any user agent to cache your redirect, then simply don't use a 301 redirect. In other words, 301 caching is here to stay, and semantically, it's a permanent redirect, so if you're planning to change the destination URL, 301 is not the right status code to use. On the other hand, 307 responses are not cached by default.

拍不死你 2025-01-05 06:48:44

在您想要 301 重定向带来的行为的情况下,例如浏览器书签的更新和 google bot 中 URL 的更改,但同时想要跟踪重定向或执行某些其他类型的功能,您可以随时添加将控制标头缓存为“无缓存”

HTTP/1.0 301 Moved Permanently
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Location: http://example.com

在 php 中,它看起来像这样:

header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Location:'.$url, true, 301);

相关:
https://stackoverflow.com/a/19003320/175071

In situations where you want the behaviour that a 301 redirect brings, like the updating of browser bookmarks and the change of URL in google bot, but at the same time want to track the redirects or perform some other kind of functionality you can always add the cache control headers to "no cache"

HTTP/1.0 301 Moved Permanently
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Location: http://example.com

In php it looks like this:

header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Location:'.$url, true, 301);

Related:
https://stackoverflow.com/a/19003320/175071

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文