從這個網址
www.example.com/error_documents/404
到這個網址
www.example.com/404
我嘗試了許多不同的 .htaccess 規則,但沒有一個奏效。我試圖從 URL 中隱藏/error_pages/檔案夾部分而不進行任何實際重定向,因為如果我寫了正確的 *RewriteRule,它只會不斷重復自己,我會收到 ERR_TOO_MANY_REDIRECTS 錯誤,因為如果你想轉到一個未知的檔案夾,錯誤檔案重定向到example.com/error_documents/404,如果我將其重寫為example.com/404,它是一個未知檔案夾,因此它試圖將我重定向到/error_documents/404頁面,但 htaccess 檔案不斷重定向到一個永遠回圈。
當前 .htaccess 檔案
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/ error_pages/([^\s] ) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^error_pages/)^(.*)$ /error_pages/$1 [L,NC]
這個 .htaccess 給了我永遠的回圈:
www.example.com/unknownfolder
到
www.example.com/error_documents/404
到
www.example.com/404
這不斷重復......
我將 cPanel 用于 ErrorDocuments,主要的 .htaccess 檔案是:
ErrorDocument 400 http://example.com/error_pages/400
ErrorDocument 401 http://example.com/error_pages/401
ErrorDocument 403 http://example.com/error_pages/403
ErrorDocument 404 http://example.com/error_pages/404
ErrorDocument 503 http://example.com/error_pages/503
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
uj5u.com熱心網友回復:
ErrorDocument 400 http://example.com/error_pages/400 ErrorDocument 401 http://example.com/error_pages/401 ErrorDocument 403 http://example.com/error_pages/403 ErrorDocument 404 http://example.com/error_pages/404 ErrorDocument 503 http://example.com/error_pages/503
首先,您不應該在ErrorDocument指令中使用絕對 URL - 這就是導致外部 (302) 重定向并公開/error_pages錯誤檔案的位置的原因。因此,這也會丟失有關導致錯誤的請求的資訊。
然而,/400和/401等應參考實際的檔案(一個或多個),該處理請求。例如。/400.html和/401.html等。
您應該對錯誤檔案使用相對于根目錄的檔案路徑(以斜杠開頭),然后 Apache 將發出內部子請求,而不是重定向。
例如:
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 503 /error_pages/503.html
您/error_pages現在對最終用戶完全隱藏。
無需手動嘗試將其從 URL 中洗掉(因為它永遠不應該出現在 URL 中)。如果需要,您可以(可選)阻止對/error_pages目錄的直接訪問(注意不要阻止對錯誤檔案的子請求)。
參考:
- https://httpd.apache.org/docs/2.4/mod/core.html#errordocument
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/341372.html
