我正在嘗試為可能存在或可能不存在的檔案或檔案夾或檔案或鏈接顯示自定義 404 頁面,但出于某種目的我仍想向訪問者顯示 404 訊息。
我已經檢查了在 htaccess 中為特定 URL設定自定義 404 頁面和在 htaccess 中為特定 URL設定自定義 404 頁面
代碼 :
RewriteRule ^foo.html$ /spoof404.txt [L,R=404,NC]
RewriteRule ^hello$ /spoof404.txt [L,R=404,NC]
RewriteRule ^file/example.php$ /spoof404.txt [L,R=404,NC]
使用這些代碼,我成功獲得了,404 Status in header但它沒有顯示spoof404.txt
筆記:
- 我不是在尋找像 404 或 403 這樣的 Errordocument
- 我不是在尋找重定向
- 這不是重復的問題。在問這里之前,我已經搜索了 stackoverflow 的其他部分。
uj5u.com熱心網友回復:
我不是在尋找像 404 或 403 這樣的 Errordocument
為什么不?這正是你這樣做的方式。僅 Apache(不使用另一個服務器端腳本)沒有其他方法可以觸發 404 HTTP 狀態并提供自定義回應。
例如:
ErrorDocument 404 /spoof404.txt
RewriteRule ^foo\.html$ - [NC,R=404]
RewriteRule ^hello$ - [NC,R=404]
RewriteRule ^file/example\.php$ - [NC,R=404]
當您請求時/foo.html,它將觸發 404 并/spoof404.txt提供服務。
當指定 3xx 范圍之外的狀態 ode 時,將忽略替換字串(因此使用單個-- 連字符)。在這種情況下,該L標志也是多余的,因為它在指定非 3xxR代碼時是隱含的。
更新:
如何使用引數進行阻塞,例如
example.com/something.php?something=tokens&othertoken=token
該RewriteRule指令僅與 URL 路徑匹配,要匹配查詢字串(第一個之后的所有內容?),那么您需要一個附加條件(RewriteCond指令)并檢查QUERY_STRING服務器變數。
例如,要匹配該確切的URL(盡管不區分大小寫)并觸發 404:
RewriteCond %{QUERY_STRING} ^something=tokens&othertoken=token$ [NC]
RewriteRule ^something\.php$ - [NC,R=404]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/407610.html
標籤:
上一篇:我收到npmERR!400錯誤請求-獲取https://artifactory.devops."**********"/artifactory/api/npm/npm/npm
