2012-02-20 101 views
3

我在我的PHP框架中使用mod_rewrite將用戶友好的鏈接重定向到應用程序的一個主入口點。 .htaccess文件我使用看起來像這樣:使用mod_rewrite和ErrorDocument

<IfModule mod_rewrite.c> 
    Options FollowSymLinks 

    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 

    RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 
</IfModule> 

現在,我想用該規則與ErrorDocument指令。我應該怎麼做?我的框架設置響應代碼404時,有一個控制器丟失:

/** 
    * Sets the HTTP response code. 
    * 
    * @param int $errorCode The response code to return. <b>Supported codes: </b>301, 307, 400, 401, 403, 404, 500, 
    * 501, 502, 503 
    */ 
    function setHTTPStatus($errorCode) { 
     $httpStatusCodes = array(301 => 'Moved Permanently', 307 => 'Temporary Redirect', 400 => 'Bad Request', 
      401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error', 
      501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable'); 

     if (array_key_exists($errorCode, $httpStatusCodes)) { 
      header("HTTP/1.0 $errorCode $httpStatusCodes[$errorCode]", true, $errorCode); 
      exit; 
     } 
    } 

我嘗試添加行ErrorDocument 404 /index.php?url=404ErrorDocument 404 /error但沒有似乎已經奏效,我得到了默認瀏覽器的錯誤頁面。

出了什麼問題?

謝謝。

回答

3

有沒有簡單的方法通過.htaccess文件來做到這一點。一旦你在php代碼域中,apache已經通過.htaccess文件和httpd.conf文件進行處理,並且向框架前端 控制器 - 在這種情況下是框架的index.php文件。

你在用什麼框架?

處理此問題的最佳方法是使用框架錯誤處理,而不是烘焙的apache處理。 apache 404適用於任何非框架服務的頁面,比如圖像,css,js等等......你的框架應該有一個配置文件來設置默認的錯誤視圖。