Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a non-cached 301 redirect?

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?

like image 487
Timo Huovinen Avatar asked Aug 30 '12 08:08

Timo Huovinen


People also ask

How do I get rid of browser cache 301 redirect?

Open the Developer Tools Panel by pressing CTRL + Shift + i on Windows or Option + Shift + i on Mac. When you see a developer tools panel open, look the the left of your URL bar to see a refresh icon. Hold the refresh icon for a few seconds until a menu appears. Then click Empty Cache and Hard Reload .

Will 301 redirects hurt SEO?

This means that 301 redirects do not harm SEO performance or reduce the “PageRank” metrics associate with a page URL – though they are not crucial to search rankings either. All 300-level server-side redirects pass PageRank to the destination page (including 301 redirects as well as 302s, and 307s).

How do I clear a permanent redirect?

Open the Chrome Developer Tools (F12) Go to the Network tab and make sure Disable cache is ticked. Reload (or navigate to) the page you no longer want to be redirected while keeping the dev tools open.


2 Answers

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:

  • 307 is the generic, temporary redirect for when a resource is moved. For example, a URL like 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.
  • 303 is the redirect that must not be cached, ever. For example, POSTing a new article to 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.
  • 302 is a bit stranger, I've never used it myself. Apparently it's more of a legacy substitute for the 303, for earlier HTTP 1.0 version clients who do not understand the 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 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;
like image 124
Core Xii Avatar answered Oct 17 '22 11:10

Core Xii


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.

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 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.

like image 5
Timo Huovinen Avatar answered Oct 17 '22 11:10

Timo Huovinen