3

我在我的 PHP 框架中使用 mod_rewrite 将用户友好的链接重定向到应用程序的一个主要入口点。我正在使用的 .htaccess 文件如下所示:

<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但似乎没有任何效果,并且我得到了默认浏览器的错误页面。

怎么了?

谢谢你。

4

1 回答 1

3

没有简单的方法可以通过 .htaccess 文件做到这一点。一旦你进入了 php 代码领域,apache 已经处理了 .htaccess 文件和 httpd.conf 文件,并移交给了框架的前端控制器——在本例中是框架的 index.php 文件。

你用的是什么框架?

处理这个问题的最好方法是使用框架错误处理,而不是内置的 apache 处理。apache 404 适用于任何非框架提供的页面,如图像、css、js 等……您的框架应该在某处有一个配置文件来设置默认错误视图。

于 2012-02-20T20:15:22.103 回答