在《域名带不带www的那些事儿》最后提到一般需要将不带www的域名通过301跳转重定向到带www的域名,设置好301跳转后最简单的验证方法就是输入不带www的网址进行测试,看看最后打开的是不是带www的网址。
更为保险的做法就是抓取数据包来分析一下,看看到底有没有实现301跳转,验证功能的同时顺便看看这些数据包里面都有些啥,服务器又是如何实现301跳转的。
首先要做的就是获取网络数据包,水石居推荐wireshark,这是一款很强大的网络封包分析软件,如果嫌装软件麻烦,可以用Check Server Headers Tool,这个只要在线就可以使用,功能比较单一。
装好wireshark后开始抓取数据,当我们输入网址http://letuknowit.com后,我们就开始向服务器请求数据了。
1、向服务其发送http header,下面是具体的内容(注意Host:的内容)
GET / HTTP/1.1
Host: letuknowit.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
2、从服务器返回301 Moved Permanently(遇到这个说明我们的301重定向生效了),服务器在返回301错误的同时还指定了要跳转到的地址(这个就是我们设置的要跳转到的地址),注意下面的Location:
HTTP/1.1 301 Moved Permanently
Date: Fri, 06 May 2011 13:51:40 GMT
Server: Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8e-fips-rhel5 mod_fcgid/2.3.5 Phusion_Passenger/2.2.15 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.17
X-Pingback: http://www.letuknowit.com/xmlrpc.php
Location: http://www.letuknowit.com/
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
3、继续请求Location中的地址(注意这里的Host:)
GET / HTTP/1.1
Host: www.letuknowit.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
4、服务器返回200 OK,说明请求成功,并返回网页数据
HTTP/1.1 200 OK
Date: Fri, 06 May 2011 13:51:42 GMT
Server: Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8e-fips-rhel5 mod_fcgid/2.3.5 Phusion_Passenger/2.2.15 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.17
X-Pingback: http://www.letuknowit.com/xmlrpc.php
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8