4

This one should be easy...

Do I need to explicitly tell PHP that I want to do a 301 redirect? Like this...

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/");
?>

Usually, I leave off the first statement and just do...

<?php
header("Location: http://www.example.com/");
?>

Would that second example actually be a 302 redirect?

  • 1
    How difficult could it be to upload both to a server and check the headers for the status code? On my Win32 Apache PHP 5.3.2 I get a 301 for the first case and 302 for second – Pranav Hosangadi Dec 15 '11 at 09:17
  • Relying on observed behavior is brittle at the very best, even though the results are correct. – Piskvor left the building Dec 15 '11 at 09:31
  • @Piskvor agreed, but you can always be sure of observed behaviour, and it is not always as described in the manual. :) – Pranav Hosangadi Dec 15 '11 at 09:34
  • Can you? I've been bitten before - "oh look, the server time is always in UTC" ... unless someone changes the server defaults, and then you spend half a day wondering, why it's working on the London server and not on the LA server. Especially in case of PHP, there are very, very few undocumented issues. – Piskvor left the building Dec 15 '11 at 09:38
  • huh? didn't quite catch that comment!! – Pranav Hosangadi Dec 15 '11 at 09:41
  • In other words: observed behavior may depend on multiple factors; not all of them are necessarily constant. The fact that something is observed in some way several times in a row doesn't guarantee that it will happen the same way next time. – Piskvor left the building Dec 15 '11 at 09:46

1 Answers1

6

Yes.

To quote the fine manual:

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

The most likely reason for this is that a 302 Found is a nonspecified-purpose redirect. There are four 3xx redirect headers that you can use.

  • 301 Moved Permanently is a permanent redirect, e.g. for keeping compatibility with old URLs. As such, many browsers will cache the redirect location, and won't check again.
  • 303 See Other is the redirect intended e.g. for a Post-Redirect-Get action - note that it's only defined as of HTTP/1.1
  • 307 Temporary Redirect means "yeah, it's usually here, but right now, the resource is somewhere else" - that may not be the meaning you want: e.g. you always want to redirect at this point. Again, defined in HTTP/1.1
  • Finally, 302 Found is a unspecified-purpose redirect - to be used when the above aren't applicable, or when HTTP/1.0 compatibility is desired (is that still a concern in 2011?); as such, it is used as the default.
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • actually, another question of mine just popped up. How does Google (and other SE's) know if the redirect was a 301 or 302? I've been reading how 301's must be used when redirecting pages since 302's are temporary, but how would they ever know the difference? Are they able to spider and read the redirect file as a 301 or a 302? I was always under the impression that PHP is unreadable by everyone except the web server – Garrett Leyenaar Dec 15 '11 at 09:55
  • @Garrett Leyenaar: PHP is unreadable; the HTTP response that your page *sends* to the client requesting it *contains* the response code. You are explicitly sending this data to the client (browser, web spider, whatever), why should the client have problems in reading data that you're sending to it? See the relevant part of the RFC; namely, Response: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6 – Piskvor left the building Dec 15 '11 at 10:02
  • ahhhhhhhh, OK that makes sense. The browser (ie client - ie spider) gets sent the status codes. Thanks again for clearing that up for me! – Garrett Leyenaar Dec 15 '11 at 10:10