Avoiding 301 redirect caching

不羁岁月 提交于 2019-11-30 17:41:35

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.

Timo Huovinen

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!