如何测试 HTTP 301 重定向?

发布于 2024-09-06 01:51:33 字数 1331 浏览 14 评论 0原文

如何轻松测试 HTTP 返回代码,例如 301 重定向?

例如,如果我想“看看发生了什么”,我可以使用 telnet 执行以下操作:

... $ telnet nytimes.com 80

Trying 199.239.136.200...
Connected to nytimes.com.
Escape character is '^]'.

GET / HTTP/1.0

(enter)

(enter)

HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Mon, 14 Jun 2010 12:18:04 GMT
Content-type: text/html
Set-cookie: RMID=007af83f42dd4c161dfcce7d; expires=Tuesday, 14-Jun-2011 12:18:04 GMT; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Cache-control: no-cache
Pragma: no-cache
Connection: close

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>    
<head>      
...

这是访问相当多信息的简单方法。

但现在我想测试一下 301 重定向是否确实是 301 重定向。

我怎样才能这样做呢?

基本上,我想知道如何获得 301,而不是得到 HTTP/1.1 200 OK

我知道我可以在浏览器中输入 URL 名称并“查看”我已重定向,但我想知道可以使用哪些工具来真正“查看”301 重定向。

顺便说一句,我确实使用 telnet 进行了测试,但是当我输入 www.example.org 时,我将其重定向到 example.org(没有 www),我只能看到“200 OK”,我不知道去看看 301。

How can one easily test HTTP return codes, like, say, a 301 redirect?

For example, if I want to "see what's going on", I can use telnet to do something like this:

... $ telnet nytimes.com 80

Trying 199.239.136.200...
Connected to nytimes.com.
Escape character is '^]'.

GET / HTTP/1.0

(enter)

(enter)

HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Mon, 14 Jun 2010 12:18:04 GMT
Content-type: text/html
Set-cookie: RMID=007af83f42dd4c161dfcce7d; expires=Tuesday, 14-Jun-2011 12:18:04 GMT; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Cache-control: no-cache
Pragma: no-cache
Connection: close

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>    
<head>      
...

Which is an easy way to access quite some infos.

But now I want to test that a 301 redirect is indeed a 301 redirect.

How can I do so?

Basically, instead of getting a HTTP/1.1 200 OK I'd like to know how I can get the 301?

I know that I can enter the name of the URL in a browser and "see" that I'm redirected, but I'd like to know what tool(s) can be used to actually really "see" the 301 redirect.

Btw, I did test with a telnet, but when I enter www.example.org, which I redirected to example.org (without the www), all I can see is an "200 OK", I don't get to see the 301.

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

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

发布评论

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

评论(6

溺深海 2024-09-13 01:51:33

我认为更方便的解决方案是使用 Curl。

只需运行:

$ curl -I http://example.com

它将返回这样的 HTTP 标头

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.1.19
Date: Sun, 21 Jul 2013 10:41:47 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://other.example.com/

A far more convenient solution in my opinion is to use Curl.

Simply run:

$ curl -I http://example.com

And it will return the HTTP headers like this

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.1.19
Date: Sun, 21 Jul 2013 10:41:47 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://other.example.com/
森林很绿却致人迷途 2024-09-13 01:51:33

好的,回答问题两分钟后我找到了答案...

执行以下操作不起作用:

telnet www.example.org 80
GET / HTTP/1.0
{enter}
{enter}

但以下操作正常:

telnet example.org 80
GET / HTTP/1.0
Host: www.example.org
{enter}
{enter}

我的错误是将 www.example.org 传递给 telnet(而不是 example.org),然后不指定任何“主机:”

现在它可以工作了,我得到这个:

Connected to xxx.xx
Escape character is '^]'.
GET / HTTP/1.0
Host: www.example.org

HTTP/1.1 301 Moved Permanently
Server: Apache-Coyote/1.1
Location: http://example.org/
Connection: close
Date: Mon, 14 Jun 2010 13:02:22 GMT
Connection: close

Connection closed by foreign host.

注意:在 Windows Vista/7 上,默认情况下不安装 Telnet 客户端。要安装它,请按照此处的说明进行操作:安装 Telnet 客户端 - Microsoft TechNet

OK, two minutes after answering the question I found the answer...

Doing the following doesn't work:

telnet www.example.org 80
GET / HTTP/1.0
{enter}
{enter}

But the following works fine:

telnet example.org 80
GET / HTTP/1.0
Host: www.example.org
{enter}
{enter}

My error was to pass www.example.org to telnet (instead of example.org) and then not specifying any "Host: ".

Now it works, I get this:

Connected to xxx.xx
Escape character is '^]'.
GET / HTTP/1.0
Host: www.example.org

HTTP/1.1 301 Moved Permanently
Server: Apache-Coyote/1.1
Location: http://example.org/
Connection: close
Date: Mon, 14 Jun 2010 13:02:22 GMT
Connection: close

Connection closed by foreign host.

Note: On Windows Vista/7, Telnet Client is not installed by default. To install it, follow the directions here: Install Telnet Client - Microsoft TechNet

蓝天白云 2024-09-13 01:51:33

测试它的一种方法是在目标网站上指定 301 重定向,并使用 curl -L 选项通知您 301 重定向。

-L, --location

(HTTP) If the server reports that the requested page has moved to a different 
location (indicated with a Location: header and a 3XX response code), this option 
will make curl redo the request on the new place. 

例如:

:~$ curl -IL mail.com

你得到:

HTTP/1.1 301 Moved Permanently
Date: Tue, 07 Feb 2017 13:17:12 GMT
Server: Apache
Location: https://www.mail.com/
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 302 Found
Date: Tue, 07 Feb 2017 13:17:13 GMT
Server: Apache
Vary: X-Forwarded-Proto,Host
Set-Cookie: cookieKID=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/
Set-Cookie: cookiePartner=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: https://www.mail.com/int/
Content-Language: en-US
Connection: close

HTTP/1.1 200 OK
Date: Tue, 07 Feb 2017 13:17:13 GMT
Server: Apache
Vary: X-Forwarded-Proto,Host,Accept-Encoding
Set-Cookie: cookieKID=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/
Set-Cookie: cookiePartner=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: JSESSIONID=964DA3BD186E264928A8C188E3BB919D; Path=/mailcom-webapp/; HttpOnly
Content-Language: en-INT
Content-Length: 74356
Connection: close
Content-Type: text/html;charset=UTF-8

One way to test it is to specify a 301 redirect on the target website and use curl -L option to inform you of 301 redirects.

-L, --location

(HTTP) If the server reports that the requested page has moved to a different 
location (indicated with a Location: header and a 3XX response code), this option 
will make curl redo the request on the new place. 

For example:

:~$ curl -IL mail.com

You get:

HTTP/1.1 301 Moved Permanently
Date: Tue, 07 Feb 2017 13:17:12 GMT
Server: Apache
Location: https://www.mail.com/
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 302 Found
Date: Tue, 07 Feb 2017 13:17:13 GMT
Server: Apache
Vary: X-Forwarded-Proto,Host
Set-Cookie: cookieKID=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/
Set-Cookie: cookiePartner=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: https://www.mail.com/int/
Content-Language: en-US
Connection: close

HTTP/1.1 200 OK
Date: Tue, 07 Feb 2017 13:17:13 GMT
Server: Apache
Vary: X-Forwarded-Proto,Host,Accept-Encoding
Set-Cookie: cookieKID=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/
Set-Cookie: cookiePartner=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: JSESSIONID=964DA3BD186E264928A8C188E3BB919D; Path=/mailcom-webapp/; HttpOnly
Content-Language: en-INT
Content-Length: 74356
Connection: close
Content-Type: text/html;charset=UTF-8
铜锣湾横着走 2024-09-13 01:51:33

Firefox 插件 HTTP Live headers 对此非常有用。

The Firefox addon HTTP Live headers is quite useful for that.

可爱咩 2024-09-13 01:51:33

在(telnet 响应的)标题中,您将在第一行看到它:

HTTP/1.1 301 Moved Permanently
Via: XXXXXXXXXXX
Connection: close
Proxy-Connection: close
Content-Length: 0
Date: Mon, 14 Jun 2010 13:03:14 GMT
Location: /xxxxxxxxx
Server: XXXXXXX
Cache-Control: private

谢谢

In the headers (of the telnet response) you'll see it in the first line:

HTTP/1.1 301 Moved Permanently
Via: XXXXXXXXXXX
Connection: close
Proxy-Connection: close
Content-Length: 0
Date: Mon, 14 Jun 2010 13:03:14 GMT
Location: /xxxxxxxxx
Server: XXXXXXX
Cache-Control: private

Thanks

始终不够爱げ你 2024-09-13 01:51:33

我使用 Firebug网络面板为此。

替代文本

I use Firebug's Net panel for this.

alt text

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