我在我的本地主機(MAMP)上的一個專案上設定了一個 404 頁面,在我的.htaccess檔案中,我包含了以下代碼:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (. ) $1.php [NC,L]
</IfModule>
ErrorDocument 404 http://localhost:8888/project/public/404.php
404.php 頁面.htaccess與專案根目錄下公共檔案夾中的檔案處于同一級別:
project
— publicFolder
— privateFolder
問題
當我在其中包含代碼<IfModule mod_rewrite.c>時,當我輸入 404 頁面 URL(即不存在的頁面)時,它會引發錯誤。當我洗掉此<IfModule mod_rewrite.c>代碼塊時,問題就消失了。錯誤資訊是:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
當我按照上述訊息中的建議檢查 apache 錯誤日志時,我得到以下資訊:
[core:error] [pid 2575] [client ::1:51038] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
我懷疑問題可能與洗掉.php所有頁面上的檔案擴展名的行有關,即RewriteRule (. ) $1.php [NC,L]
我的問題
<IfModule mod_rewrite.c>當用戶轉到應該是 404 頁面時,如何獲取塊內的干凈 URL 代碼以停止引發內部服務器錯誤?
非常感謝任何幫助。
uj5u.com熱心網友回復:
您的問題只是您確實實作了無休止的重寫回圈。
嘗試使用帶有附加條件的變體:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(. )$ $1.php [L]
您需要了解L您正在使用的標志確實終止了重寫,但僅限于當前回圈。每當重寫周期改變(重寫)已處理的請求時,就會開始一個新的周期。直到請求穩定,所以不再重寫。由于重寫的目標再次匹配您的條件和規則的匹配模式,您再次在您的實作中重寫請求。這最終會導致對類似的請求,/foo.php.php.php.php.php為每個重寫周期附加一個“.php”。
另一種方法是在[END]此處使用較年輕的 ? 標志而不是較舊的[L]標志。END 標志完全終止重寫程序,忽略任何其他規則。因此,您需要確保該規則確實是最后應該應用的規則。使用該標志您將不需要額外的條件,因為完全終止顯然可以防止無休止的重寫回圈。
一般來說,我建議不要為此使用分布式組態檔(“.htaccess”)。但是要在實際http服務器的中央主機配置中實作這樣的規則。并在那里使用絕對路徑。
這通常可以防止很多混亂和副作用。
當然,這里可能還有其他問題無法從您提出的問題中立即弄清楚。但上面是一個明顯的問題。
始終使用新的匿名瀏覽器視窗進行測驗,始終在瀏覽器中進行深度重新加載,以防止測驗時客戶端快取影響。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511155.html
標籤:阿帕奇干净的网址404 页
