What does HTTP/1.1 302 mean exactly?
我曾经读过的一篇文章说这意味着跳跃(从一个URI到另一个URI),但是即使实际上根本没有跳跃,我也检测到了" 302"!
302重定向表示该页面已被临时移动,而301重定向则表明该页面已被永久移动。
301对SEO值有好处,而302并不是因为301会指示客户端忘记原始URL的值,而302则保留原始值,因此可以通过创建两个逻辑上不同的URL来潜在地减少值每个都产生相同的内容(搜索引擎将它们视为不同的重复项,而不是具有两个名称的单个资源)。
早在RFC 2616还在徘徊的时候就问过这个问题。该问题的一些答案都基于此类文档,该文档现已不再相关。引用Mark Nottingham的名字,在撰写本文时,他是IETF HTTP和QUIC工作组的联合主席:
Don’t use RFC2616. Delete it from your hard drives, bookmarks, and burn (or responsibly recycle) any copies that are printed out.
下列定义了HTTP / 1.1协议的文档已取代了旧的RFC 2616:
- RFC 7230:消息语法和路由
- RFC 7231:语义和内容
- RFC 7232:条件请求
- RFC 7233:范围请求
- RFC 7234:缓存
- RFC 7235:身份验证
因此,我旨在提供一个基于RFC 7231的答案,它是HTTP / 1.1状态代码的当前参考。
在后续请求中,Web浏览器可能从
这是在RFC 7231中定义
6.4.3. 302 Found
The
302 (Found) status code indicates that the target resource
resides temporarily under a different URI. Since the redirection
might be altered on occasion, the client ought to continue to use the
effective request URI for future requests.The server SHOULD generate a
Location header field in the response
containing a URI reference for the different URI. The user agent MAY
use theLocation field value for automatic redirection. The server's
response payload usually contains a short hypertext note with a
hyperlink to the different URI(s).Note: For historical reasons, a user agent MAY change the request
method fromPOST toGET for the subsequent request. If this
behavior is undesired, the307 (Temporary Redirect) status code
can be used instead.
根据Mozilla的MDN Web文档,
The Web page is temporarily not available for reasons that have not been unforeseen. That way, search engines don't update their links.
其他状态代码用于重定向
RFC 7231定义了以下状态代码以进行重定向:
-
301 (永久移动) -
302 (找到) -
307 (临时重定向)
创建RFC 7238是为了定义另一个状态代码以进行重定向:
-
308 (永久重定向)
有关更多详细信息,请参考此答案。
查看HTTP 301与302重定向的一种简单方法是:
假设您有一个到" http://sample.com/sample"的书签。您使用浏览器去那里。
此时,将302重定向到其他URL意味着您应该将书签保留在" http://sample.com/sample"。这是因为目标URL将来可能会更改。
301重定向到其他URL意味着您的书签应更改为指向新URL,因为它是永久重定向。
从RFC 2616(超文本传输??协议规范)中:
1 2 3 4 5 6 7 8 9 10 11 12 | 10.3.3 302 Found The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field. The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s). |
资源:
http://www.ietf.org/rfc/rfc2616.txt
从维基百科:
The HTTP response status code 302
Found is the most common way of
performing a redirection. It is an
example of industrial practice
contradicting the standard.
就SEO而言,301和302都不错,取决于情况,
如果只能返回一个版本(即另一个版本重定向到该版本),那就太好了!此行为是有益的,因为它减少了重复的内容。在重定向到尾部斜杠URL的特定情况下,我们的搜索结果可能会显示具有200响应代码的URL版本(最常见的是尾部斜杠URL)-无论重定向是301还是302。
302是指示资源位置更改的响应-"找到"。
资源现在应该位于的URL应该在响应" Location"标头中。
"跳转"应由发出请求的客户端完成(对响应Location标头字段中的资源url进行新请求)。
根据http状态代码定义,302表示(临时)重定向。
"请求的资源临时驻留在其他URI下"
HTTP代码302用于重定向,请参见http://en.wikipedia.org/wiki/HTTP_302。
它告诉浏览器阅读页面到其他地方并加载另一个页面。它的用法很常见。
根据RFC 1945 /超文本传输??协议-HTTP / 1.0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 302 Moved Temporarily The requested resource resides temporarily under a different URL. Since the redirection may be altered on occasion, the client should continue to use the Request-URI for future requests. The URL must be given by the Location field in the response. Unless it was a HEAD request, the Entity-Body of the response should contain a short note with a hyperlink to the new URI(s). If the 302 status code is received in response to a request using the POST method, the user agent must not automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued. Note: When automatically redirecting a POST request after receiving a 302 status code, some existing user agents will erroneously change it into a GET request. |
302状态代码是HTTP响应状态代码,指示请求的资源已被临时移动到其他URI。由于将来可能会更改位置或当前重定向指令,因此收到302 Found响应代码的客户端应继续将原始URI用于将来的请求。
具有此状态代码的HTTP响应还将在标头字段Location中提供URL。这是对用户代理(例如网络浏览器)的邀请,该请求是对位置字段中指定的新URL的另一次相同请求。最终结果是重定向到新URL。
- 代码302指示临时重定向。
- 区别于301重定向的最显着特征之一是,在302重定向的情况下,SEO的强度不会转移到新的URL。
- 这是因为已将这种重定向设计为在需要将内容重定向到不是最终页面的页面时使用。因此,一旦取消了重定向,原始页面就不会失去其在Google搜索引擎中的位置。
示例:尽管我们发现自己需要302重定向并不是很常见,但在某些情况下此选项可能非常有用。这些是最常见的情况:
- 当我们意识到页面上有一些不适当的内容时。在解决问题的同时,我们可以将用户重定向到可能感兴趣的另一个页面。
-
如果对我们网站的攻击需要恢复
在任何网页中,此重定向都可以帮助我们最大程度地减少
发生率。
重定向302是一个代码,用于告知特定URL的访问者该页面已被临时移动,将他们直接定向到新位置。
-
换句话说,当Google机器人或其他搜索引擎请求加载特定页面时,重定向302被激活。此时,由于此重定向,服务器返回了指示新URL的自动响应。
-
这样就避免了搜索引擎的错误和烦恼
和用户,以确保顺利导航。
有关更多详细信息,请参阅本文。