PHP header('位置:' . $url) http1.0 vs http1.1 vs?

发布于 2024-11-01 09:31:34 字数 432 浏览 6 评论 0原文

我正在使用 PHP 进行一些重定向,例如:

header('Location: '.$url);

但现在我需要使用其他一些状态代码进行一些重定向。

我应该为此使用http1.0还是http1.1。或者我们已经达到了 http2.0 或更高版本?

两者的状态代码是否相同?

假设我想使用状态代码 404(未找到)进行重定向。

我可以这样做吗:

header('HTTP/1.1 404 Not Found');
header('Location: '.$url);

或者有一个:

header('HTTP/2.0 404 Not Found');
header('Location: '.$url);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦 2024-11-08 09:31:34

一般来说,浏览器使用的是 HTTP 1.1(HTTP 1.0 已经很老了;并且不支持当今非常需要的几个有趣的功能)

HTTP 2.0?不存在这样的事情;-)

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.

是的,阅读本文可能需要一些时间——但如果您每天都在使用 HTTP,那么了解一点可能是个好主意;-)

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 ;-)

感受沵的脚步 2024-11-08 09:31:34

没有HTTP/2.0; HTTP/1.1 是最新版本。

您可以使用 $_SERVER['SERVER_PROTOCOL'] 的值以相同的协议版本进行响应:

header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');

此时您应该意识到,如果响应是 HTTP/1.0,则 HTTP/1.1 标头字段可能将被忽略。

顺便说一句:Location标头字段 仅针对状态代码 201 和 3xx 定义:

对于 201(已创建)响应,位置是请求创建的新资源的位置。对于 3xx 响应,位置应该指示服务器自动重定向到资源的首选 URI。

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.

撩起发的微风 2024-11-08 09:31:34

请参阅 header() 的第三个参数 用于发送特定状态代码:

http_response_code

强制 HTTP 响应代码为指定值。请注意,此参数仅在字符串不为空时才有效。

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.

我一直都在从未离去 2024-11-08 09:31:34

如果您发送 HTTP/1.0 还是 HTTP/1.1,这取决于您,因为对于您的目的来说,没有任何改变。

Its up to you, if you send HTTP/1.0 or HTTP/1.1, because for your purposes nothing changed.

_蜘蛛 2024-11-08 09:31:34

使用HTTP/1.1。目前称为 HTTPbis 的 HTTP 的下一个版本将只是一个更新,仍然称为HTTP/1.1。

Location: 标头独立于协议版本工作。请注意,并非所有 Status: 号码都允许使用它们。只有范围 200-400 才应该使用它。对于 500 个错误,它肯定会被忽略。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文