2 回答

TA貢獻1784條經驗 獲得超8個贊
我發現這個有效:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L] #if not already index.php
RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)
RewriteRule . /index.php [L] #redirect to index.php
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) https://www.example.com/%{REQUEST_FILENAME} [R=301,L] #redirect to https://www
</IfModule>
這不可能像所寫的那樣“工作”,因為存在許多錯誤:
您缺少打開
<IfModule mod_rewrite.c>
指令。然而,無論如何,這個<IfModule>
包裝都不是必需的,應該被刪除。Apache 不支持行結束注釋。具體來說,以下行將由于“錯誤的標志分隔符”而導致 500 錯誤:
RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)
(更新:如果您在此處沒有看到 500 錯誤響應,則您可能使用的是 LiteSpeed 服務器;而不是 Apache?在 LiteSpeed 上,此行尾注釋似乎按預期工作?。?/p>
www
除了對目錄(包括根目錄)或真實文件(除了index.php
)的請求之外,重定向到的外部重定向(最后)永遠不會被處理。在現有重寫之前,需要先進行此重定向。然而,請看下一點......您在目標 URL 中錯誤地使用了
REQUEST_FILENAME
(絕對文件系統路徑) - 這將導致格式錯誤的重定向。您可以使用REQUEST_URI
服務器變量(完整的 URL 路徑),但請注意,您還存在雙斜杠問題。因此,它需要像下面這樣重寫:RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
小要點:
RewriteBase
這里沒有使用它,可以安全地刪除。(除非你有其他指令使用這個?)
概括
綜合以上幾點我們有:
RewriteEngine On
# Redirect to https://www
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Stop here if already index.php
RewriteRule ^index\.php$ - [L]
# Only if NOT a FILE (non-existent file)
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite to index.php (in the document root)
RewriteRule . /index.php [L]
請注意,這仍然會將目錄重寫為,與您的評論/index.php所述相反。
首先使用 302(臨時)重定向進行測試以避免潛在的緩存問題。
在測試之前您需要清除瀏覽器緩存。

TA貢獻1853條經驗 獲得超18個贊
經過大量的修改/研究,我發現這個有效:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L] #if not already index.php
RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)
RewriteRule . /index.php [L] #redirect to index.php
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) https://www.example.com/%{REQUEST_FILENAME} [R=301,L] #redirect to https://www
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
- 2 回答
- 0 關注
- 118 瀏覽
添加回答
舉報