【问题标题】:Using mod_rewrite with ErrorDocument将 mod_rewrite 与 ErrorDocument 一起使用
【发布时间】:2012-02-20 20:07:18
【问题描述】:

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

怎么了?

谢谢。

【问题讨论】:

    标签: php model-view-controller mod-rewrite errordocument


    【解决方案1】:

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

    你用的是什么框架?

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

    【讨论】:

      猜你喜欢
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 2011-11-13
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      相关资源
      最近更新 更多