咱們知道,某些狀況下因爲網站遷移,權限,頁面錯誤時咱們可能須要跳轉頁面到錯誤頁面,如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.權限檢測,攔截,重要文件下載(先判斷權限,而後跳轉到文件連接,相似某視頻網站的)