Do I need to use http redirect code 302 or 307?
Asked Answered
A

4

13

Suppose I have a page on my website to show media releases for the current month
http://www.mysite.com/mediareleases.aspx

And for reasons which it's mundane to go into*, this page MUST be given a query string with the current day of the month in order to produce this list:
http://www.mysite.com/mediareleases.aspx?prevDays=18

As such I need to redirect clients requesting http://www.mysite.com/mediareleases.aspx to http://www.mysite.com/mediareleases.aspx?prevDays=whateverDayOfTheMonthItIs

My question is, if I want google to index the page without the query parameter, should I use status code 302 or 307 to perform the redirect?

Both indicate that the page has "temporarily" moved - which is what I want because the page "moves" every day if you get my meaning.

[*] I'm using a feature of a closed-source .NET CMS so my hands are tied.

Alexina answered 18/3, 2010 at 5:12 Comment(3)
Do you have to perform some action to get to this page from the main page? I.e. submit a form or something?Exhilarative
No, essentially it will all be handled by the server. If someone comes in on a page with no query string, the browser will be redirected to the same page with the query string included to give the right date range.Alexina
Whatever redirect you do, a <link rel="canonical"> on the resulting page may be no bad idea.Aetolia
T
19

Google's documentation seems to indicate that both 302 and 307 are treated equivalently, and that "Googlebot will continue to crawl and index the original location."

But in the face of ambiguity, you might as well dig into the RFCs and try to do the Right Thing, with the naïve hope that the crawlers will do the same. In this case, RFC 2616 § 10.3 contains nearly identical definitions for each response code, with one exception:

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

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

Which does not strike me as a significant distinction. My reading is that 302 instructs clients that webmasters are untrustworthy, and 307 explicitly tells webmasters that clients will not trust them, so they may freely alter the redirect.

I think the more telling point is the note in 302's definition:

Note: RFC 1945 and RFC 2068 specify that the client is not allowed to change the method on the redirected request. However, most existing user agent implementations treat 302 as if it were a 303 response, performing a GET on the Location field-value regardless of the original request method. The status codes 303 and 307 have been added for servers that wish to make unambiguously clear which kind of reaction is expected of the client.

Which, to me, indicates that 302 and 307 are largely equivalent, but that HTTP/1.0 clients failed to implement 302 correctly the first time around.

Topee answered 18/3, 2010 at 6:39 Comment(0)
A
11

Short answer: neither. In most cases the code you really want to use is 303.

For the long answer, first we need some background.

When getting a redirect code the client can (A) load the new location using the same request type or (B) it can overwrite it and use GET.

The HTTP 1.0 spec did not have 303 and 307, it only had 302, which mandated the (A) behavior. But in practice it was discovered that (A) led to a problem with submitted forms.

Say you have a contact form, the visitor fills it and submits it and the client gets a 302 to a page saying "thanks, we'll get back to you". The form was sent using POST so the thanks page is also loaded using POST. Now suppose the visitor hits reload; the request is resent the same way it was obtained the first time, which is with a POST (and the same payload in the body). End result: the form gets submitted twice (and once more for every reload). Even if the client asks the user for confirmation before doing that, it's still annoying in most cases.

This problem became so prevalent that client producers decided to override the spec and issue GET requests for the redirected location. Basically, it was an oversight in the HTTP 1.0 spec. What clients needed most was a 303 (and behavior (B) above), but instead they only got 302 (and (A)).

If HTTP 1.0 would have offered both 302 and 303 there would have been no problem. But it didn't, so it resulted in a 302 which nobody used correctly. So HTTP 1.1 added 303 (badly needed) but also decided to add 307, which is technically identical to 302, but is a sort of "explicit 302"; it says "yeah, I know the issues surrounding 302, I know what I'm doing, give me behavior (A)".

Now, back to our question. You see now why in most cases you will want 303.

Cases where you want to preserve the request type are very rare. And if you do find yourself such a case, the answer is simple: use 302. Either the client speaks HTTP 1.0, in which case it can't understand 307; or it speaks HTTP 1.1, which means it has no reason to preserve the rebelious behavior of old clients ie. it implements 302 correctly, so use it!

Arie answered 29/12, 2012 at 0:41 Comment(3)
What happens when a HTTP 1.0 client sees 303?Symphonia
This answer gives some good background information on the development of 302 and 303/307, but the analysis and advice is quite wrong. Far from being "very rare", preserving the request type is common; what the specification is designed to address (moved resources); and what the OP asked about. Trying to change URLs by issuing 303s can break a site. A POST to oldlocation/myform will become a GET to newlocation/myform, which is not the desired behavior. A 303 is appropriate in the special case of doing form submission redirects, it should not be used for moving resources.Fraud
Why was 307 even added then if you should always use 302 for preserving the request type?Newby
B
7

5 years on... note that the behaviour of 307 has been updated by RFC-7231#6.4.7 in June 2014, and is now significantly different from a 302, in that the method may not change:

The 307 (Temporary Redirect) status code indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI.

Probably not an issue for the original question, but may be relevant to others who come across this question just looking for the difference.

Brutalize answered 17/3, 2015 at 6:2 Comment(0)
R
0

I feel your pain. As for a solution, it's hard to say what search engines will do. It seems that each one has its own way of handling redirects. This link suggests that a 302 will index the contents of the redirected page but still use the main page link, but it's not clear what a 307 will do.

Another way you could consider proceeding is with a javascript redirect and a <noscript> tag explaining what's going on. That will also foul up non-javascript browsers, and you'd have to proceed with caution to avoid Google's sneaky-site detection routine, but I suspect that as long as your noscript contains a hyperlink that matches the new URL you'd be OK.

Either way I'd still pursue doing a purely server-side request if at all possible. Heck, if your expected traffic is light, you could treat your home page as a proxy in the case where there's no querystring. Have it use a background thread to request itself with the querystring and pipe out the results. :-)

edit just saw you're using .NET. Maybe consider this answer from SO: C# Can i modify Request.Form's variables? .

Risarise answered 18/3, 2010 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.