當使用者輸入 https://example.com/~quux/foo(無結尾斜線)時,Apache 預設會嘗試尋找名為 foo 的檔案而非目錄,導致 404 或觸發 301 重導向至 /~quux/foo/,造成額外請求與相對路徑資源載入錯誤。
/ 結尾,Apache 才會尋找 DirectoryIndex(如 index.html)。/~quux/image.gif 而非 /~quux/foo/image.gif)。RewriteEngine on RewriteBase /~quux/ RewriteRule ^foo$ foo/ [R]
[R]:發送 302/301 外部重導向,瀏覽器網址列更新為 /~quux/foo/,後續相對路徑解析正確。.htaccess 或主設定檔 httpd.conf。RewriteEngine on
RewriteBase /~quux/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
RewriteCond %{REQUEST_FILENAME} -d:僅當請求路徑對應實體目錄存在時觸發。^(.+[^/])$:匹配不以斜線結尾的非空路徑。$1/:在捕獲路徑後補上斜線。httpd.conf 的 <Directory> 區塊或站點 .htaccess 中加入上述規則。mod_rewrite 已載入:LoadModule rewrite_module modules/mod_rewrite.so。AllowOverride All 允許 .htaccess 覆寫。systemctl restart httpd 或 apachectl graceful。RewriteCond %{REQUEST_URI} !^/api/。