PHP标题('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 this. 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);
我正在使用PHP进行一些重定向,例如: p>
header('Location:'。$ url);
code> pre>
但现在我需要使用其他一些状态代码进行重定向。 p>
我应该http1.0或http1.1。 或者我们已经处于http2.0或更高版本? p>
两者的状态代码是否相同? p>
所以我想说我要重定向 使用状态码404(未找到)。 p>
我可以这样做: p>
header('HTTP / 1.1 404 Not Found');
header('Location:'。$ url);
code> pre>
或者是否有: p>
标头( 'HTTP / 2.0 404 Not Found');
header('Location:'。$ url);
code> pre>
div>
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.
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 -- and going through RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1 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 ;-)
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.