如果有人訪問我的網站,例如:
example.comwww.example.comhttp://example.comhttp://www.example.comhttps://example.comhttps://www.example.com
我想將其重定向到https://www.example.com/index.html. 我嘗試了下面的代碼:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/index.html$1 [R=301,L,NE]
上面的代碼作業正常,但是如果訪問者型別https://www.example.com它不會重定向到https://www.example.com/index.html,并且如果訪問者使用谷歌瀏覽器并輸入www.example.com它也不會重定向到https://www.example.com/index.html.
uj5u.com熱心網友回復:
請嘗試以下操作:
RewriteEngine On
# Redirect "/" to "/index.html" (HTTPS www)
RewriteRule ^$ https://www.example.com/index.html [R=301,L]
# Redirect non-www to www ( HTTPS)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS (any remaining URLs)
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
只有第一條規則滿足您問題中概述的要求。所有宣告的 URL 都被重定向到https://www.example.com/index.html(HTTPS www)。
第二條和第三條規則只是針對其他任何內容的通用規范(www HTTPS)重定向,包括用戶是否請求/index.html。
首先使用 302(臨時)重定向進行測驗,以避免潛在的快取問題。您需要在測驗前清除瀏覽器快取。
看看你的規矩...
RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/index.html$1 [R=301,L,NE]
您的第一條規則重定向到 HTTP,而不是 HTTPS,這是預期目標。
第二條規則將所有內容(包括任何靜態資產)重定向到/index.html請求的 URL 并將其附加到末尾(使用$1反向參考)。例如。一個請求http://example.com/foo將被重定向到https://www.example.com/index.htmlfoo(在兩個重定向中)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/438105.html
標籤:.htaccess http 重定向 改写 https
上一篇:遍歷Observable物件陣列并將來自另一個Observable的附加資料添加到每個物件
下一篇:htaccess在404時重定向
