PHP header('Location: ' . $url) http1.0 vs http1.1 vs?
I'm doing some redirects using PHP, e.g.:
header('Location: '.$url);
But now I need to do some redirects with some other statuscodes.
Should I http1.0 or http1.1 for th开发者_JAVA百科is. Or are we already at http2.0 or greater?
And are the status code the same for both?
So let's say I want to redirect using statuscode 404 (Not Found).
Can I just do:
header('HTTP/1.1 404 Not Found');
header('Location: '.$url);
Or is there a:
header('HTTP/2.0 404 Not Found');
header('Location: '.$url);
Generally speaking, browsers are using HTTP 1.1 (HTTP 1.0 is quite old ; and doesn't support several interesting features which are pretty much required nowadays)
HTTP 2.0 ? There is no such thing ;-)
Don't hesitate to take a look at [Hypertext Transfer Protocol][1] -- and going through [**RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1**][2] can be a good idea.
Yes, it can take some time to read this -- but if you are working every day with HTTP, knowing a bit about it can be a good idea ;-)
There is not HTTP/2.0; HTTP/1.1 is the latest version.
You can use the value of $_SERVER['SERVER_PROTOCOL']
to respond with the same protocol version:
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
At this point you should be aware that if the response is HTTP/1.0 the HTTP/1.1 header fields probably will be ignored.
By the way: The Location header field is only defined for the status codes 201 and 3xx:
For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource.
See the third parameter to header()
for sending a specific status code:
http_response_code
Forces the HTTP response code to the specified value. Note that this parameter only has an effect if the string is not empty.
Its up to you, if you send HTTP/1.0
or HTTP/1.1
, because for your purposes nothing changed.
Use HTTP/1.1
. The next version of HTTP currently known as HTTPbis will just be an update, and still be called HTTP/1.1.
The Location:
header works independently from the protocol version. Note that not all Status:
numbers allow them. Only ranges 200-400 should use it. For 500 errors it will certainly be ignored.
精彩评论