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?
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 to domain.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:
<?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 stop agents from caching a 301 as well, if you must, using the above cache control headers like this:
header_no_cache();
header_redirect_permanent($url);
or simply 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;