咱们知道,某些状况下因为网站迁移,权限,页面错误时咱们可能须要跳转页面到错误页面,如html
1.HTTP/1.1 404 Not Foundjava
2.HTTP/1.1 301 Moved Permanentlyweb
3.HTTP/1.1 302 Moved Temporarily编程
可是咱们发送响应码只是一种状态机制,所以每每咱们须要选择合适的跳转方式,Location跳转是一种常见的方式,特别对于作SEO的同窗来讲json
下面咱们经过php语言来讲明(结果与编程语言无关)服务器
index.php编程语言
<?php header('HTTP/1.1 301 Moved Permanently'); Header( "Location: http://192.168.1.98/result.php?".$_SERVER['QUERY_STRING'] ); exit();
result.php优化
<?php $arr = []; $arr['name'] = 'zhangsan'; $arr['age'] = 20; echo json_encode($arr); ?>
当咱们访问http://192.168.1.98/index.php?st=123ABC时,发生了以下转换网站
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.权限检测,拦截,重要文件下载(先判断权限,而后跳转到文件连接,相似某视频网站的)