标签:
我们知道,某些情况下由于网站迁移,权限,页面错误时我们可能需要跳转页面到错误页面,如
1.HTTP/1.1 404 Not Found
2.HTTP/1.1 301 Moved Permanently
3.HTTP/1.1 302 Moved Temporarily
但是我们发送响应码只是一种状态机制,因此往往我们需要选择合适的跳转方式,Location跳转是一种常见的方式,特别对于做SEO的同学来说
下面我们通过php语言来说明(结果与编程语言无关)
index.php
<?php header(‘HTTP/1.1 301 Moved Permanently‘); Header( "Location: http://localhost/test/result.php" ); exit();
result.php
<?php $arr = []; $arr[‘name‘] = ‘zhangsan‘; $arr[‘age‘] = 20; echo json_encode($arr); ?>
当我们访问index.php时,发生了如下转换
Request-1
GET /test/ HTTP/1.1 Host: localhost Connection: keep-alive Pragma: no-cache Cache-Control: no-cache User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36 Content-Type: text/plain; charset=utf-8 Accept: */* Accept-Encoding: gzip, deflate, sdch Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
RedirectTo
HTTP/1.1 301 Moved Permanently Date: Sun, 30 Aug 2015 09:59:14 GMT Server: Apache/2.4.9 (Win32) PHP/5.5.12 X-Powered-By: PHP/5.5.12 Location: http://localhost/test/result.php Content-Length: 0 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html
Request-2
GET /test/ HTTP/1.1 Host: localhost Connection: keep-alive Pragma: no-cache Cache-Control: no-cache User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36 Content-Type: text/plain; charset=utf-8 Accept: */* Accept-Encoding: gzip, deflate, sdch Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
responseTo
HTTP/1.1 200 OK Date: Sun, 30 Aug 2015 09:59:14 GMT Server: Apache/2.4.9 (Win32) PHP/5.5.12 X-Powered-By: PHP/5.5.12 Content-Length: 28 Keep-Alive: timeout=5, max=99 Connection: Keep-Alive Content-Type: text/html
响应体
{"name":"zhangsan","age":20}
总结:
在这个过程中Request-1与Request-2相同,也就是说Location在跳转的时候同样将请求进行了转发,在这一过程中,一个请求被传递了2次,跳转的过程中并未改变客户端连接,整个过程是完整的,所以Location使得http请求过程具有完整性和连续性
使用方向:
1.SEO优化,固定重定向
2.请求转发跳转
3.权限检测,拦截,重要文件下载(先判断权限,然后跳转到文件链接,类似某视频网站的)
标签:
原文地址:http://my.oschina.net/ososchina/blog/499242