How to avoid browser redirect caching

11,039

Solution 1

To quote the HTTP specification on the 301 status code:

301 Moved Permanently

The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise.

Hence, the fact that browsers cache the redirected URI shouldn't be a surprise, since it's done as specified.

To get around this problem, most sites use 302 (307 could also be used).

Solution 2

Here's one more relevant quote from HTTP/1.1 spec (in addition to @Bruno 's answer):

13.4 Response Cacheability

...

A response received with a status code of 200, 203, 206, 300, 301 or 410 MAY be stored by a cache and used in reply to a subsequent request, subject to the expiration mechanism, unless a cache-control directive prohibits caching.

https://www.rfc-editor.org/rfc/rfc2616#section-13.4

Share:
11,039

Related videos on Youtube

George Boot
Author by

George Boot

Experiences: HTML / CSS / Javascript PHP (mainly Laravel) MySQL Swift / Cocoa Touch Node JS Spare time: In spare time also enthusiast survival player :-)

Updated on July 06, 2022

Comments

  • George Boot
    George Boot almost 2 years

    It's not the fist time that i stuck to this problem. Everytime when i use an 301 redirect, the browser caches it. When i change the redirect in my app, i manualy have to clear my browsers cache.

    This is not very welcome for an website as you will understand. Is there an solution to avoid this caching?

    Currently i got an list of new redirects from my customer. In my .htaccess, i replaced the old ones for the new ones, but it doesn't toke effect.

    When i cleared my browser cache, it seems to work propper.

    Any solutions? Thanks a lot!

  • George Boot
    George Boot over 12 years
    Thanks! It was first ment as a permanent, not as a temporary redirect. As i read the 301 specifications i understand the meaning of it, and that the answer is clear: you just can't because 301 is not temporary but permanent.