我已經建立了一個 php 網站,除了http://版本之外,所有重定向都可以正常作業。
所以我試圖編輯我的主 .htaccess 檔案以防止http://顯示錯誤“不安全”,并將用戶轉發到https://
我在構建網站時使用了一個名為 TraversyMVC 的框架,它在 public_html 目錄 .htaccess 檔案中具有以下設定。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
這是我在 /public 目錄中的 .htaccess 檔案
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(. )$ index.php?url=$1 [QSA,L]
</IfModule>
這是我在 public_html 目錄 .htaccess 檔案中嘗試過的,但它不起作用:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} public/ [L]
RewriteRule ^(.*)$ https://www.mywebsite.com/public/$1 [L]
</IfModule>
尋找正確的解決方案。
謝謝
uj5u.com熱心網友回復:
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} public/ [L] RewriteRule ^(.*)$ https://www.mywebsite.com/public/$1 [L]
您在內部重寫之前/public將請求重定向到子目錄(因此檢查的第二個條件public/永遠不會匹配)并且/public不應該在您重定向到的可見 URL 中。您還應該明確指出這是外部重定向,而不是內部重寫。(如所寫,此規則將導致隱式 302 - 臨時 - 重定向。)
這需要在根.htaccess檔案中的現有規則之前進行。請嘗試以下操作:
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
首先使用 302(臨時)重定向進行測驗,以避免潛在的快取問題。
您不需要<IfModule>包裝器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/459227.html
