將http請求轉發至https的URL
重定向
Redirect [status] URL-path URL
status狀態:
Permanent: 返回永久重定向狀態碼 301
Temp:返回臨時重定向狀態碼302. 此為默認值
示例:
Redirect temp / https://www.magedu.com/
從客戶端發送瀏覽請求--至服務器--從服務器回復一個重定向的網頁地址到客戶端---從客戶端發送請求到服務器--從服務器回復請求,通過四次完成一整次訪問
1 vim /etc/httpd/conf/httpd.conf 2 ---- 3 4 <virtualhost *:80 > 5 DocumentRoot /data/asite 6 CustomLog "logs/asite_access_log" combined 7 servername www.a.com 8 <directory "/data/asite"> 9 Require all granted 10 </directory> 11 redirect Permanent / http://www.b.com 12 </virtualhost> 13 -------- 14#在a網站的語句塊的配置文件中添加這行,表示當你訪問根的時候,跳轉到www.b.com
15 systemctl restart httpd
16#重啟服務
通過curl命令測試:只顯示了301 ,curl 命令默認只發一次請求,不會主動發第二次請求,重定向需要發第二次請求的
1 curl http://www.a.com 2 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 3 <html><head> 4 <title>301 Moved Permanently</title> 5 </head><body> 6 <h1>Moved Permanently</h1> 7 <p>The document has moved <a href="http://www.b.com">here</a>.</p> 8 </body></html>
1 curl -L http://www.a.com 2 b.site
curl命令默認只發送一次請求,-L會根據用戶發過來的響應碼在執行操作,如301就會在發送一次請求跳轉到對應的網站上
一般從http跳轉到https都是用的臨時重定向
1 <virtualhost *:80 > 2 DocumentRoot /data/asite 3 CustomLog "logs/asite_access_log" combined 4 servername www.a.com 5 <directory "/data/asite"> 6 Require all granted 7 </directory> 8 redirect temp / https://www.a.com 9 </virtualhost>
#搜索引擎會根據永久重定向來抓取頁面,臨時重定向會抓取,永久重定向不會抓取舊的網站
直接在配置文件中將http跳轉到https會產生循環
DocumentRoot "/var/www/html" redirect temp / https://www.a.com
多主機的模式可以用這種,
curl -Lk http://www.a.com curl: (47) Maximum (50) redirects followed
1 vim /etc/httpd/conf/httpd.conf 2---- 3 RewriteEngine on 4 RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=302] 5 #RewriteEngine on重寫引擎 6 #RewriteRule重寫規則, ^(/.*)$ 頭尾中間/任意字符,表示訪問這個網站的任意字符串,給你重定向到https://%{HTTP_HOST},HTTP_HOST就是你訪問的主機頭,$1后項引用,使用302臨時跳轉
重新測試:
1 curl -Lk http://www.a.com 2 www.alex.com