A while ago all browsers changed their behaviour and started caching 301 redirects, I would like to know how to do a 301 redirect that is not cached in php?
相关问题
- Views base64 encoded blob in HTML with PHP
- Angular RxJS mergeMap types
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
A http status code 301 without caching can be used to do URL canonicalization while retaining the tracking functionality.
To prevent a 301 redirect from being cached just set the cache control headers, then you can undo the redirect and clients (bot and browsers) will no longer get redirected.
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 undo the redirect by redirecting back to the original URL without causing infinite loops or other nonsense.
This does not in any way mean that the 301 code has to be used for all redirects, on the contrary, different kinds of redirect have different status codes which Core Xii summarized.
Core Xii's answer is correct.
However to add to that you can use the Firefox/Chrome Developer Tools plugin to clear redirect and DNS caches.
301 is a permanent redirect, so caching makes sense. If your redirect isn't permanent, use 307 (temporary redirect), 302 (found) or 303 (see other).
See here for the appropriate use cases.
To elaborate on the differences between these three:
domain.com/news/latest
might do a 307 redirect to the latest news article,domain.com/news/article-594873
. Since this temporary redirection may persist for a while (that particular article may be the latest for several hours), browsers might cache the redirect. To control the degree to which they do, use cache control headers.domain.com/news
might create a new news article, and a 303 redirect to it is provided todomain.com/news/article-978523
. Since another POST request results in a completely different, new article being created, it cannot be cached.Since you asked specifically about PHP:
You can stop agents from caching a 301 as well, if you must, using the above cache control headers like this:
or simply add