我有一個名為 的子目錄/marker,里面有一些 png 檔案。現在,我希望在/marker/standard.png嘗試呼叫不存在的 png 時回傳檔案。換句話說:如果,例如,/marker/doesntexist.png被呼叫(它不存在),/marker/standard.png應該被傳遞。
聽起來很簡單,但幾次嘗試RewriteRule還是ErrorDocument失敗了。
有任何想法嗎?蒂亞
uj5u.com熱心網友回復:
如果請求目錄中不存在的/marker/standard.png另一個.png檔案,您可以執行以下操作。/marker
這使用 mod_rewrite 并且應該靠近.htaccess檔案根目錄中的檔案頂部。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^marker/(?!standard\.png$). \.png$ marker/standard.png [L]
我采取了額外的步驟來排除對自身的請求standard.png(使用否定的前瞻),以防由于某種原因不存在。
請注意,這是一個區分大小寫的字串比較。例如。僅.png匹配檔案,不匹配.PNG.
這自然會在standard.png服務時產生 200 OK 回應。如果您仍然需要 404 Not Found 狀態,ErrorDocument請改用(請參閱下一節)。
使用ErrorDocument
您還提到了ErrorDocument指令的使用。這是另一種解決方案,盡管它會導致 404 HTTP 回應狀態,這可能是可取的,也可能不是可取的。
例如,為了將其限制ErrorDocument在子目錄中,請在此子目錄中/marker創建一個附加.htaccess檔案,其中包含以下內容:
ErrorDocument 404 /marker/standard.png
這自然會服務standard.png于對/marker子目錄的任何不存在的請求。要將其限制為.png僅請求,然后將其包裝ErrorDocument在<If>運算式中。例如:
<If "%{REQUEST_URI} =~ /\.png$/">
ErrorDocument 404 /marker/standard.png
</If>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/438106.html
標籤:.htaccess http-status-code-404
