How to do redirects without caching 301?

Some time ago, all browsers changed their behavior and started caching 301 redirects, I would like to know how to make a 301 redirect that is not cached in php?

+8
source share
3 answers

301 is a constant redirect, so caching makes sense. If your redirect is not permanent, use 307 (temporary redirect), 302 (found) or 303 (see Others).

See here for relevant use cases.

To clarify the differences between the three:

  • 307 is a general temporary redirect when a resource moves. For example, a URL such as domain.com/news/latest might redirect 307 to the latest news article domain.com/news/article-594873 . Since this temporary redirect may persist for a while (this particular article may be the last for several hours), browsers may cache the redirect. To control the extent to which they are executed, use the cache control headers.
  • 303 is a redirect that cannot be cached ever. For example, POSTing a new article at domain.com/news can create a new news article, and a 303 redirect to it is provided by domain.com/news/article-978523 . Since another POST request results in a completely different article being created, it cannot be cached.
  • 302 is a little weirder, I never used it myself. Apparently, this is more of a substitute for legacy 303, for earlier HTTP version 1.0 clients that do not understand 303.

Since you asked specifically about PHP:

 <?php function header_redirect_permanent($url) { header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently', true, 301); header('Location: ' . $url); } function header_no_cache() { 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'); // past date to encourage expiring immediately } 

You can also stop agents from caching 301, if necessary, using the above cache control headers, for example:

 header_no_cache(); header_redirect_permanent($url); 

or just add

 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); exit; 
+23
source share

The non-caching http status code 301 can be used to canonicalize URLs while maintaining tracking functionality.

To prevent 301 redirects, simply set the cache control headers, then you can cancel the redirection, and clients (bot and browsers) will no longer be redirected.

 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); exit; 

This is useful when you want browsers to update the URL in bookmarks and bots to update their index, but still be able to track them or cancel the redirect by redirecting back to the original URL without causing endless loops or other nonsense.

This in no way means that the code 301 should be used for all redirects, on the contrary, different types of redirects have different status codes, which Core Xii generalizes.

+5
source share

The answer to Core Xii is correct.

However, to add to this, you can use the Firefox / Chrome developer plugin to clear redirects and DNS caches.

+1
source share

All Articles