ErrorDocument

时间:2015-12-30 10:09:46

标签: apache .htaccess errordocument

我正在为我的API构建一个小框架,因为它们非常具体,但是当我收到ErrorDocument的数据时,我遇到了Content-Type的问题。目前,我有以下.htaccess:

<IfModule mod_headers.c>
Header set Content-Type "text/plain"
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE"
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
RewriteRule ^([a-z]+)(/[A-Za-z0-9-\._\/]*)?$ $1.php [QSA,L]

ErrorDocument 404 "API_NOT_FOUND"
</IfModule>

我想要实现的是具有不同Content-Type的错误404。 text / plain或application / json都可以,但这些都不行。所以我可能无法像我想的那样在.htaccess中设置Content-Type标头。我也尝试将ErrorDocument作为文件,但由于该目录的路径是动态的,我不能使用错误文档,而不使用硬编码的路径:

ErrorDocument 404 /api/index.php?error=404

.htaccess位于api目录中,但可以重命名目录。有什么方法可以实现以下其中一项吗?

  • 在.htaccess中设置Content-Type,以便ErrorDocument没有带有字符集的text / html。
  • 将错误文档设置为.htaccess目录中的index.php。

如果第一个有效,我仍然可以在.php脚本中覆盖它吗?我的一些调用是JSON,其他是XML文件。

2 个答案:

答案 0 :(得分:2)

您可以使用ForceType指令。

首先使用以下数据在error.json内创建一个名为DocumentRoot/folder/的文件:

{"error":"API_NOT_FOUND"}

然后在你的DocumentRoot/folder/.htaccess中有这样的话:

ErrorDocument 404 /folder/error.json
<Files "/folder/error.json">
   ForceType application/json
</Files>

答案 1 :(得分:0)

感谢您的回答,并且抱歉最近提供了最终答案。我找到了一个解决方案,我认为应该这样做。

<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

RewriteRule ^([A-Za-z0-9_-]+)(/[A-Za-z0-9-\._\/]*)?$ $1.php [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z]+) index.php?error=404 [L]
</IfModule>

错误被重定向到index.php,它在完成日志记录后会输出正确的内容,所以我认为这是一个双赢的局面。为简单说明,将在index.php中执行以下行:

http_response_code(404);
die(json_encode(['error' => ['code' => 'API_SCRIPT_NOT_FOUND', 'number' => 404]]);

编辑:我将解释我做的很多事情。 index.php通常会生成一个文档,但是当index.php没有被称为clean时,我会输出notfound错误。它看起来像这样:

<?php
  class Documentation {}
  $API = new Documentation();

  require_once('common/initialize.php');

  Output::notfound('API_SCRIPT_NOT_FOUND');
?>

输出类是一个小类,它使用正确的Content-Type处理输出。它会自动设置&application; / json&#39;没有设置其他Content-Type时。一个小例子(有更多的功能,但这是它运行的功能):

class Output {
    protected static $instance = null;
    public static function instance() {
        return self::$instance ?: self::$instance = new static;
    }

    private $finished = false;

    private function finish($output, $status = null) {
        if($this->finished) return; $this->finished = true;

        http_response_code($status ?: 200); $content = null;

        $headers = headers_list();
        foreach($headers as $header) {
            if(substr($header, 0, 13) == 'Content-Type:') {
                $content = substr($header, 14); break;
            }
        }

        if(!$content && !headers_sent()) {
            header(sprintf('Content-Type: %s', $content = 'application/json'));
            die(json_encode((http_response_code() >= 400) ? ['error' => $output] : $output));
        }

        die(!empty($output['code']) ? $output['code'] : $output);
    }

    public static function notfound($output) { self::instance()->finish(['code' => $output, 'number' => 404], 404); }
}