How to test an HTTP 301 redirect?
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.
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/
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
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
The Firefox addon HTTP Live headers is quite useful for that.
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
I use Firebug's Net panel for this.
精彩评论