HTTP/1.1 302 到底是什么意思?
我曾经读过的一篇文章说这意味着跳转(从一个 URI 到另一个 URI),但我检测到这个“302”,即使实际上根本没有跳转!
Some article I read once said that it means jumping (from one URI to another), but I detected this "302" even when there was actually no jumping at all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
这个问题很久以前就被提出了,当时 RFC 2616 仍然存在。 这个问题的一些答案是基于这样的文件,现在已经不再相关了。 引用马克·诺丁汉,他在撰写本文时是联合主席IETF HTTP 和 QUIC 工作组:
旧的 RFC 2616 已被以下文档取代,这些文档共同定义了 HTTP/1.1 协议:
并且,截至 2022 年 6 月,一组新的RFC 废弃了上面列出的文档:
因此,我的目标是根据 RFC 9110,这是 HTTP 语义的当前参考。
302
状态代码的响应302
是执行 URL 重定向的常见方式。 以及302
状态代码,响应应包含位置
具有不同 URI 的标头。 此类标头将由用户代理解析,然后执行重定向:Web 浏览器可能会从
POST
到GET
。 如果不希望出现此行为,307
可以使用(临时重定向)状态代码来代替。这就是
302
状态的方式代码在 RFC 9110 中定义:根据 Mozilla 的 MDN Web 文档,一个典型的用例[
302
]302] 是:用于重定向的其他状态代码
RFC 9110 定义了以下用于重定向的状态代码(其中一些状态代码最初是在其他 RFC 中定义的,但已全部合并在 RFC 9110 中) :
301
:永久移动302
:找到307
:临时重定向308
:永久重定向请参阅此回答了解更多详情。
This question was asked a long ago, while the RFC 2616 was still hanging around. Some answers to this question are based in such document, which is no longer relevant nowadays. Quoting Mark Nottingham who, at the time of writing, co-chairs the IETF HTTP and QUIC Working Groups:
The old RFC 2616 has been supplanted by the following documents that, together, define the HTTP/1.1 protocol:
And, as of June 2022, a new set of RFCs obsoleted the documents listed above:
So I aim to provide an answer based in the RFC 9110, which is the current reference for the HTTP semantics.
The
302
status codeA response with
302
is a common way of performing URL redirection. Along with the302
status code, the response should include aLocation
header with a different URI. Such header will be parsed by the user agent and then perform the redirection:Web browsers may change from
POST
toGET
in the subsequent request. If this behavior is undesired, the307
(Temporary Redirect) status code can be used instead.This is how the
302
status code is defined in the RFC 9110:According to MDN web docs from Mozilla, a typical use case for [
302
]302] is:Other status codes for redirection
The RFC 9110 defines the following status codes for redirection (some of these status codes were originally defined in other RFCs, but have all been consolidated in the RFC 9110):
301
: Moved Permanently302
: Found307
: Temporary Redirect308
: Permanent RedirectRefer to this answer for further details.
查看 HTTP 301 与 302 重定向的一个简单方法是:
假设您有一个书签“http://sample.com/样本”。 您使用浏览器前往那里。
此时,302 重定向到不同的 URL 意味着您应该将书签保留为“http://sample.com/sample ”。 这是因为目标 URL 将来可能会发生变化。
301 重定向到不同的 URL 意味着您的书签应该更改为指向新的 URL,因为它是永久重定向。
A simple way of looking at HTTP 301 vs. 302 redirects is:
Suppose you have a bookmark to "http://sample.com/sample". You use a browser to go there.
A 302 redirect to a different URL at this point would mean that you should keep your bookmark to "http://sample.com/sample". This is because the destination URL may change in the future.
A 301 redirect to a different URL would mean that your bookmark should change to point to the new URL as it is a permanent redirect.
来自 RFC 2616(超文本传输协议规范):
来源:
http://www.ietf.org/rfc/rfc2616.txt
From RFC 2616 (the Hypertext Transfer Protocol Specification):
Source:
http://www.ietf.org/rfc/rfc2616.txt
来自维基百科:
From Wikipedia:
根据 http 状态代码定义,302 表示(临时)重定向。
“请求的资源暂时驻留在不同的 URI 下”
As per the http status code definitions a 302 indicates a (temporary) redirect.
"The requested resource resides temporarily under a different URI"
302是指示资源位置改变的响应-“找到”。
资源现在所在的 URL 应位于响应的“Location”标头中。
“跳转”应该由请求客户端完成(向响应 Location 标头字段中的资源 url 发出新请求)。
302 is a response indicating change of resource location - "Found".
The url where the resource should be now located should be in the response 'Location' header.
The "jump" should be done by the requesting client (make a new request to the resource url in the response Location header field).
在SEO方面,301和302都很好,这取决于具体情况,
如果只能返回一个版本(即另一个版本重定向到它),那就太好了! 此行为是有益的,因为它减少了重复的内容。 在重定向到尾部斜杠 URL 的特定情况下,我们的搜索结果可能会显示带有 200 响应代码的 URL 版本(最常见的是尾部斜杠 URL)——无论重定向是 301 还是 302。
In the term of SEO , 301 and 302 both are good it is depend on situation,
If only one version can be returned (i.e., the other redirects to it), that’s great! This behavior is beneficial because it reduces duplicate content. In the particular case of redirects to trailing slash URLs, our search results will likely show the version of the URL with the 200 response code (most often the trailing slash URL) -- regardless of whether the redirect was a 301 or 302.
HTTP 代码 302 用于重定向,请参阅 http://en.wikipedia.org/wiki/HTTP_302 。
它告诉浏览器阅读一个页面去其他地方并加载另一个页面。 它的用法很常见。
HTTP code 302 is for redirection see http://en.wikipedia.org/wiki/HTTP_302.
It tells the browse reading a page to go somewhere else and load another page. Its usage is very common.
根据 RFC 1945/超文本传输协议 - HTTP / 1.0:
According to RFC 1945/Hypertext Transfer Protocol - HTTP / 1.0:
示例:- 虽然我们发现自己需要 302 重定向的情况并不常见,但此选项在某些情况下非常有用。 这些是最常见的情况:
对于任何页面,此重定向可以帮助我们最大限度地减少
发生率。
302 重定向是一种代码,可告知特定网址的访问者该页面已暂时移动,从而将他们直接定向到新位置。
换句话说,当 Google 机器人抓取时,会激活 302 重定向或其他搜索引擎请求加载特定页面。 此时,由于此重定向,服务器会返回一个自动响应,指示新的 URL。
通过这种方式,可以避免搜索引擎出现错误和烦恼
和用户,保证顺利导航。
有关更多详细信息,请参阅此文章。
EXAMPLE:- Although it is not very common that we find ourselves in need of a 302 redirect, this option can be very useful in some cases. These are the most frequent cases:
of any of the pages, this redirect can help us minimize the
incidence.
A redirect 302 is a code that tells visitors of a specific URL that the page has been moved temporarily, directing them directly to the new location.
In other words, redirect 302 is activated when Google robots or other search engines request to load a specific page. At that moment, thanks to this redirection, the server returns an automatic response indicating a new URL.
In this way errors and annoyances are avoided both to search engines
and users, guaranteeing smooth navigation.
For More details Refer this Article.
302 状态代码是 HTTP 响应状态代码,指示所请求的资源已暂时移动到不同的 URI。 由于位置或当前重定向指令将来可能会发生更改,因此收到 302 Found 响应代码的客户端应继续使用原始 URI 来进行将来的请求。
具有此状态代码的 HTTP 响应将在标头字段 Location 中另外提供一个 URL。 这是对用户代理(例如网络浏览器)的邀请,以向位置字段中指定的新 URL 发出第二个(其他方面相同)请求。 最终结果是重定向到新 URL。
A 302 status code is HTTP response status code indicating that the requested resource has been temporarily moved to a different URI. Since the location or current redirection directive might be changed in the future, a client that receives a 302 Found response code should continue to use the original URI for future requests.
An HTTP response with this status code will additionally provide a URL in the header field Location. This is an invitation to the user agent (e.g. a web browser) to make a second, otherwise identical, request to the new URL specified in the location field. The end result is a redirection to the new URL.
对于任何可能对命名感到好奇的人,我只想补充一点,它可能被称为“Found”,因为用户打算接收的主要资源(例如,私人网页)当时不可用(例如,用户尚未证明其身份),因此服务器发现用户可以接收的新资源(这是最常见用例中的登录页面) )。
而且它以捉迷藏的方式“迷失并找到”,这意味着在 302 状态下丢失的资源只是暂时丢失,它不应该永远丢失(除非玩家有一些不良意图;))。
For anyone who might be curious about the naming, I'm just going to add that it's probably called "Found" because the main resource(e.g., a private web page) the user intends to receive is not available at that moment(e.g., the user has not proved their identity yet), so instead the server has found a new resource that the user can receive(which is a login page in the most common use case).
Also it's "getting lost and found" in the hide-and-seek manner, meaning a lost resource under a 302 status is only lost temporarily, it is not supposed to be lost forever(unless a player has some bad intentions;)).
首先让我们看一个场景 301 和 302 如何工作
想象一下有一些资源,例如 --> http://hashcodehub.com/user ,现在将来我们将把资源名称更改为 user- info - -> 现在网址应该是 http://hashcodehub.com/user-info --> 但用户仍在尝试访问相同的 URL --> http://hashcodehub.com/user --> 从后端我们可以将用户重定向到新的 url 并发送状态代码 301 --> 用于永久移动。
上面我已经解释了 301 的工作原理,
让我们了解 302 在现实生活中如何使用
302 --> 临时重定向 --> 这里不需要更改完整的 url,但由于某种原因,我们重定向到不同位置的资源。 在位置标头字段中,我们将给出新资源 url 的值,浏览器将再次向响应位置标头字段中的资源 url 发出请求。
可以使用 302,以防万一我们的页面上有不合适的内容。在解决该问题的同时,我们可以将所有使用的内容重定向到某个临时 url 并解决该问题。
如果网站上有一些附件并且某些页面需要恢复,也可以使用它,在这种情况下我们也可以将用户重定向到不同的资源。
重定向302例如用于使主页具有不同语言的多个版本。主要版本可以是英语;也可以是英文版本。 但如果访问者来自其他国家/地区,那么该系统会自动将他们重定向到使用其语言的页面。
First lets take a scenario how 301 and 302 works
Imagine there is some resource like --> http://hashcodehub.com/user , now in future we are changing the resouce name to user- info --> now the url should be http://hashcodehub.com/user-info --> but the user is still trying to access the same URL --> http://hashcodehub.com/user --> here from the backend we can redirect the user to the new url and send the status code as 301 --> which is used for permanently moved.
Above I have explained how 301 Works
Lets understand how 302 will be used in real life
302 --> Temporary redirection --> here the complete url does not need to be changed but for some reason we are redirecting to resource at different locations. Here in the location header field we will give the value of the new resource url browser will again make the request to the resource url in the response location header field.
302 can be used just in case if there is something not appropriate content on our page .While we solve that issue we can redirect all our used to some temporary url and fix the issue.
It can also be used if there is some attach on the website and some pages requires restoration in that case also we can redirect the user to the different resource.
The redirect 302 serves, for example, to have several versions of a homepage in different languages.The main one can be in English; but if the visitors come from other countries then this system automatically redirects them to page in their language.
302:当您尝试加载的资源或页面通过 302 重定向暂时移动到其他位置时,就会出现此状态代码。
302 : this status code occurs when a resource or page you're attempting to load has been temporarily moved to a different location -- via a 302 redirect.
我知道 302 有一些用例,我认为与 http -> 相比,它表示临时重定向是非常合适的。 https 永久。
Google 使用 302 将其服务重定向到与您对应的语言/国家/地区的版本。
cdn(例如 npm 使用的 unpkg.com)使用 302 将文件重定向到其当前/确切版本,该版本将来会发生变化。
当电子商务网站进行季节性促销时,它希望在该期间显示,但在其他时间返回原始页面。
I know 302 have some use cases, which I think it is quite appropriate to mean temporary redirection, compared to http -> https which is permanent.
google uses 302 to redirect their services to the versions in the language/country that corresponds to you.
cdn (e.g. unpkg.com used by npm) uses 302 to redirect the file to its current/exact version, which will change in the future.
When an e-commerce website has a seasonal promotion it wants to show during that period but back to the original page other times.
302 重定向意味着页面被暂时移动,而 301 意味着页面被永久移动。
301 有利于 SEO 价值,而 302 则不然,因为 301 会指示客户端忘记原始 URL 的值,而 302 会保留原始 URL 的值,因此可能会通过创建两个逻辑上不同的 URL 来降低价值每个都产生相同的内容(搜索引擎将它们视为不同的重复项,而不是具有两个名称的单个资源)。
A 302 redirect means that the page was temporarily moved, while a 301 means that it was permanently moved.
301s are good for SEO value, while 302s aren't because 301s instruct clients to forget the value of the original URL, while the 302 keeps the value of the original and can thus potentially reduce the value by creating two, logically-distinct URLs that each produce the same content (search engines view them as distinct duplicates rather than a single resource with two names).