如何测试HTTP 301重定向?

11

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

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

... $ telnet nytimes.com 80

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

GET / HTTP/1.0

(按下回车键)

(按下回车键)

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(我将其重定向到没有www的example.org)时,我只能看到“200 OK”,我无法看到301。

6个回答

22

据我个人看法,更方便的解决方案是使用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/

3

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

以下操作无效:

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),然后没有指定任何"Host: "

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

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

3

测试其方法之一是在目标网站上指定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

1
在 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

谢谢


实际上,你不是以我在问题中展示的方式来做,这也是我提问的重点 :) 请看我的答案,了解为什么用telnet尝试并没有起作用,以及需要做些什么才能使其正常工作 :) - NoozNooz42
你在第一行没有301是因为它不是一个301。如果“www”版本可以,但“非www”版本不行,那么这不是telnet或我的答案的问题,而是其中一个URL可以,另一个URL不行。这是两个不同的URL。 - Mike Gleason jr Couturier

1

啊,有趣,谢谢你提供那个链接。不过我正在寻找更加“底层”的东西,比如telnet :) 有趣的是,我在发完问题两分钟后就找到了解决方法 :) - NoozNooz42

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接