當嘗試讓 example.com/about 顯示 example.com/about.html 並且不將 url 更改為 /about.html 時,最好的選擇是什麼?現在我正在嘗試以下程式碼,它只會返回 404 錯誤。
RewriteRule ^/about$ https://example.com/about.html [R=301,L]
編輯 #1 至w3dk我目前確實啟用了多視圖,但仍然收到 404 錯誤。這是我目前在 VirtualHost 中的設置
<Directory /var/www/public_html>
Options All +MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
答案1
而不是使用 mod_rewrite 來內部重寫對於請求,您可以只使用 MultiViews (mod_negotiation) 來代替:
Options +MultiViews
mod_rewrite 讓您可以進行更複雜的 URL 重寫,但是,如果您所做的只是刪除檔案副檔名,那麼 MultiViews 就足夠了 - 這就是它的設計目的。
當您發出請求/about
(有效目錄中沒有擴展名的 URL/檔案)時,請啟用 MultiViews,mod_negotiation 將搜尋與預期 mime 類型相符的檔案並將其作為內部請求傳回。
更新:
Options All +MultiViews
這不是有效的語法(我假設您必須使用 Apache 2.2,因為在 Apache 2.4 上啟動期間這會失敗並出現錯誤)。如中所述阿帕奇文檔:
警告
Options
與 a+
或與 without 混合使用-
是無效的語法,並且可能會導致意外結果。
要表達All
andMultiViews
你需要兩個指令:
Options All
Options +MultiViews
All
是預設值(在 Apache 2.2 上),因此這可能不是必需的。但是,最好在單一指令中僅指定所需的選項,例如:
Options FollowSymLinks Includes MultiViews
答案2
你可以這樣嘗試嗎:我認為它會起作用
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^about$ https://example.com/about.html [R=301,L]
您也可以使用重定向:
Redirect 301 /about http://example.com/about.html
Redirect 301 /any_dir/about http://example.com/about.html