【问题标题】:Different ways of resolving the WWW URL prefix解析 WWW URL 前缀的不同方法
【发布时间】:2013-04-30 07:05:41
【问题描述】:

似乎最普遍接受的将用户从http://example.com 重定向到http://www.example.com 的方法是通过301 永久重定向,但我遇到了一个似乎正在使用其他方法重定向的站点(Facebook)。 我使用了 PHP 内置的 get_headers() 函数,当我希望看到两个不同的 HTTP 代码(类似于 200 OK 和 301 永久重定向)时,它显示 http://facebook.comhttp://www.facebook.com 返回 302(找到)。那么,如果 facebook.com 重定向到 www.facebook.com,为什么我会看到相同的 HTTP 响应代码?他们还可以使用哪些其他方法(不包括元刷新等明显的方法)来重定向?

以下是测试 get_headers() 的数据数组,带和不带 WWW 前缀:

Array
(
[0] => HTTP/1.0 302 Found
[1] => Location: https://facebook.com/
[2] => Content-Type: text/html; charset=utf-8
[3] => X-FB-Debug: wwltbGRu1BTbywd3gta2SdLc+wpyGdq51OOfn2wGKPs=
[4] => Date: Tue, 30 Apr 2013 06:46:59 GMT
[5] => Connection: close
[6] => Content-Length: 0
[7] => HTTP/1.0 301 Moved Permanently
[8] => Location: https://www.facebook.com/
[9] => Content-Type: text/html; charset=utf-8
[10] => X-FB-Debug: WxvPFmdvhZu01Ksi4H9ttx0nffFCraY9TQtxscHgRlU=
[11] => Date: Tue, 30 Apr 2013 06:47:00 GMT
[12] => Connection: close
[13] => Content-Length: 0
[14] => HTTP/1.0 302 Found
[15] => Location: http://www.facebook.com/unsupportedbrowser
[16] => P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
[17] => X-Content-Type-Options: nosniff
[18] => Set-Cookie: datr=5Gh_Ue6q0jyzse8jKRbcPg2N; expires=Thu, 30-Apr-2015 06:47:00 GMT; path=/; domain=.facebook.com; httponly
[19] => Content-Type: text/html; charset=utf-8
[20] => X-FB-Debug: 3lKH0JMHOjd/q5qzs0s6h+WJdk0YnQG67DhnqJa7D3Q=
[21] => Date: Tue, 30 Apr 2013 06:47:00 GMT
[22] => Connection: close
[23] => Content-Length: 0
[24] => HTTP/1.0 200 OK
[25] => Cache-Control: private, no-cache, no-store, must-revalidate
[26] => Expires: Sat, 01 Jan 2000 00:00:00 GMT
[27] => P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
[28] => Pragma: no-cache
[29] => X-Content-Type-Options: nosniff
[30] => X-Frame-Options: DENY
[31] => X-XSS-Protection: 0
[32] => Set-Cookie: datr=5Gh_URQUdhoRh4w74JHDNmtA; expires=Thu, 30-Apr-2015 06:47:00 GMT; path=/; domain=.facebook.com; httponly
[33] => Set-Cookie: reg_ext_ref=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.facebook.com
[34] => Set-Cookie: reg_fb_gate=http%3A%2F%2Fwww.facebook.com%2Funsupportedbrowser; path=/; domain=.facebook.com
[35] => Set-Cookie: reg_fb_ref=http%3A%2F%2Fwww.facebook.com%2Funsupportedbrowser; path=/; domain=.facebook.com
[36] => Content-Type: text/html; charset=utf-8
[37] => X-FB-Debug: poI8PSRn+KNx3GnMc+8ZcZzsWFncr28gewEWkLisCbc=
[38] => Date: Tue, 30 Apr 2013 06:47:00 GMT
[39] => Connection: close
[40] => Content-Length: 19409
)
Array
(
[0] => HTTP/1.0 302 Found
[1] => Location: https://www.facebook.com/
[2] => Content-Type: text/html; charset=utf-8
[3] => X-FB-Debug: duXMWSWa3Fr5k98z6Ze/HkXeYG5qY8tRuhvCEXg/6wQ=
[4] => Date: Tue, 30 Apr 2013 06:47:00 GMT
[5] => Connection: close
[6] => Content-Length: 0
[7] => HTTP/1.0 302 Found
[8] => Location: http://www.facebook.com/unsupportedbrowser
[9] => P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
[10] => X-Content-Type-Options: nosniff
[11] => Set-Cookie: datr=5Wh_UUqT6BOIQhG3lvSCTnUI; expires=Thu, 30-Apr-2015 06:47:01 GMT; path=/; domain=.facebook.com; httponly
[12] => Content-Type: text/html; charset=utf-8
[13] => X-FB-Debug: B7P9WpeO6QRPr3g+7D/b6w3ssnJMGVa3AXd3qaG6UOA=
[14] => Date: Tue, 30 Apr 2013 06:47:01 GMT
[15] => Connection: close
[16] => Content-Length: 0
[17] => HTTP/1.0 200 OK
[18] => Cache-Control: private, no-cache, no-store, must-revalidate
[19] => Expires: Sat, 01 Jan 2000 00:00:00 GMT
[20] => P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
[21] => Pragma: no-cache
[22] => X-Content-Type-Options: nosniff
[23] => X-Frame-Options: DENY
[24] => X-XSS-Protection: 0
[25] => Set-Cookie: datr=5Wh_Ud4XwmSGX2Uxzg_KAHgW; expires=Thu, 30-Apr-2015 06:47:01 GMT; path=/; domain=.facebook.com; httponly
[26] => Set-Cookie: reg_ext_ref=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.facebook.com
[27] => Set-Cookie: reg_fb_gate=http%3A%2F%2Fwww.facebook.com%2Funsupportedbrowser; path=/; domain=.facebook.com
[28] => Set-Cookie: reg_fb_ref=http%3A%2F%2Fwww.facebook.com%2Funsupportedbrowser; path=/; domain=.facebook.com
[29] => Content-Type: text/html; charset=utf-8
[30] => X-FB-Debug: 4HXkUp4MHgy+i8qppuZvbHhYH6F4xQlfl0mUNrpj5hY=
[31] => Date: Tue, 30 Apr 2013 06:47:01 GMT
[32] => Connection: close
[33] => Content-Length: 19409
)

【问题讨论】:

    标签: php http redirect http-headers http-status-code-302


    【解决方案1】:

    如果您使用 https://facebook.com/(第 1 步)而没有 www,首先 Facebook 会将您重定向到 https://www.facebook.com/(第 2 步),然后检查您的浏览器(第 3 步;它会检查您的用户代理:如果您使用 curl 放置一个典型的浏览器用户代理,如 Chrome):如果不支持浏览器,它会将您重定向到 http://www.facebook.com/unsupportedbrowser(第 4 步)。但是,如果您直接使用https://www.facebook.com/,即从第 2 步开始,那么它会立即检查您的浏览器(第 3 步),如果不支持,它只会将您重定向到 http://www.facebook.com/unsupportedbrowser(第 4 步)。

    此页面解释 HTTP 状态代码定义

    (我记下了我认为是您正在寻找的答案的文字)

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

    10.3.2 301 永久移动

    已为请求的资源分配了一个新的永久 URI 和任何 未来对该资源的引用应该使用返回的一个 URI。具有链接编辑功能的客户端应该自动 将对 Request-URI 的引用重新链接到一个或多个新的 尽可能由服务器返回的引用。这个回应是 除非另有说明,否则可缓存。

    新的永久 URI 应该由 回复。除非请求方法是 HEAD,否则 响应应该包含一个简短的超文本注释,其中包含指向 新的 URI。

    如果收到 301 状态代码以响应请求,而不是 GET 或 HEAD,用户代理不能自动重定向 请求,除非它可以被用户确认,因为这可能 更改发出请求的条件。

      Note: When automatically redirecting a POST request after
      receiving a 301 status code, some existing HTTP/1.0 user agents
      will erroneously change it into a GET request.
    

    10.3.3 302 找到

    请求的资源暂时位于不同的 URI 下。 由于重定向有时可能会改变,客户端应该 继续使用 Request-URI 来处理未来的请求。这个回应是 仅在 Cache-Control 或 Expires 标头指示时可缓存 字段。

    临时 URI 应该由 回复。除非请求方法是 HEAD,否则 响应应该包含一个简短的超文本注释,其中包含指向 新的 URI。

    如果收到 302 状态代码以响应请求,而不是 GET 或 HEAD,用户代理不能自动重定向 请求,除非它可以被用户确认,因为这可能 更改发出请求的条件。

      Note: RFC 1945 and RFC 2068 specify that the client is not allowed
      to change the method on the redirected request.  However, most
      existing user agent implementations treat 302 as if it were a 303
      response, performing a GET on the Location field-value regardless
      of the original request method. The status codes 303 and 307 have
      been added for servers that wish to make unambiguously clear which
      kind of reaction is expected of the client.
    

    【讨论】:

    • 我的问题是关于 HTTP 响应代码,我理解重定向,但我不明白为什么 HTTP 代码不应该是相同的。
    • 看不懂的可以再发消息给我解释一下
    • 正如@mohammadmohsenipur 所述,您应该设置用户代理。如果您使用 get_headers,您可能会发现此讨论很有用:stackoverflow.com/questions/8403025/…
    猜你喜欢
    • 1970-01-01
    • 2012-04-10
    • 2011-01-13
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    相关资源
    最近更新 更多