Wouter
Wouter

Reputation: 1987

How can I create a temporary Redirect in .htaccess that all browsers adhere to?

Multiple variations of this question have been asked, but I can't seem to find a satisfying answer.

I add the following to my .htaccess file

Redirect 302 /test1 /test

Then browse to the website. I get redirected to /test.
I then remove the redirect, and browse to the website again. In Chrome, Firefox and Internet Explorer (Latest versions on 2017-04-14) the browsers use the cached redirect, and send me to /test. On Edge, I get /test1.

As mentioned in other questions, this is kindof ok, per the RFC:

Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests.

Note that "SHOULD" in RFC lingo means that it is advised, but not obligatory. So it is advised not to cache the redirect (and hence, go to /test1), but it is ok to cache it anyway. I don't understand why browsers would not follow the RFC suggestion here, ...but there's that.

Then there is the 303 Redirect:

The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.

I have tested this. given the RFC, this redirect MUST NOT (as in ever) be cached. Edge and Firefox play nice here, and don't cache it. Internet Explorer and Chrome seem to decide to bluntly ignore the RFC here, and keep on sending me to /test.

Are these known bugs?
Am I misunderstanding the RFC?
Is there any way, to make sure my Redirects don't get cached?

Is the only solution using a RewriteRule? Browsers can't cache the target URL's there, because they never see them, and tend to not cache the target location, so this works for me. (Thanks to @Olaf Dietsche)

RewriteRule ^test1 /test

(In my use-case, I want to redirect something like website.com/file.pdf to another file. There is no HTML page I can add a no-cache parameter to. Users type this URL into their browsers manually)

Upvotes: 0

Views: 359

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

the RFC (2616) states

This document has been superseded.

Precisely, it is RFC 7231, which explicitly states "Obsoletes: 2616". And in this case the relevant sections are 303 See Other and especially Appendix B. Changes from RFC 2616

The description of the 303 (See Other) status code has been changed to allow it to be cached if explicit freshness information is given, and a specific definition has been added for a 303 response to GET. (Section 6.4.4)

So in some cases, caching a 303 response is allowed.

Upvotes: 2

Related Questions