AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 786053
Accepted
Bob Ortiz
Bob Ortiz
Asked: 2016-06-25 04:55:05 +0800 CST2016-06-25 04:55:05 +0800 CST 2016-06-25 04:55:05 +0800 CST

当访问所有其他文件被拒绝时,如何将所有 HTTP 错误代码动态发送到一个 PHP 文件?

  • 772

我有以下情况,我正在开发一个 API。我将所有流量重写为一个名为: 的 PHP 脚本route.php,使用mod_rewrite如下:

1: RewriteEngine On
2: RewriteCond %{REQUEST_FILENAME} -f [OR]
3: RewriteCond %{REQUEST_FILENAME} -d
4: RewriteRule ^.* route.php [L]

其他文件不应该被访问,这就是为什么我route.php只使用白名单来访问。因此我使用这个:

order allow,deny
<FilesMatch "route\.php">
    allow from all
</FilesMatch>

我想将所有 1xx、2xx(200 除外)、4xx 和 5xx HTTP 状态代码发送到 PHP 脚本(假设error.php?code=404显示该状态代码的动态错误页面。在这种情况下,我可能必须允许访问error.php也在FilesMatch部分。

我找到了部分我想要的东西,如本文所述,但我无法实现或设法让它按照我上面描述的方式工作。

我的目的是error.php显示一个 JSON 输出(基于状态码动态),{'statusCode':'404','status':'Not Found'}包括我使用的所有常见(安全)HTTP 标头。

.htaccess web-applications mod-rewrite custom-errors errordocument
  • 1 1 个回答
  • 1797 Views

1 个回答

  • Voted
  1. Best Answer
    Bob Ortiz
    2016-07-12T12:31:04+08:002016-07-12T12:31:04+08:00

    这个问题没有引起太多关注,我自己回答了这个问题。所以特此回答。这可以通过以下方式实现。欢迎任何改进。

    下面应该在.htaccess文件中:

    order allow,deny
    deny from all
    <FilesMatch "^route\.php$">
        # replace 0.0.0.0 with IP that is allowed to access the Matched files
        allow from 0.0.0.0
    </FilesMatch>
    
    ErrorDocument 400     /error.php?code=400
    ErrorDocument 401     /error.php?code=401
    ErrorDocument 403     /error.php?code=403
    ErrorDocument 404     /error.php?code=404
    ErrorDocument 500     /error.php?code=500
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteRule ^route.php/(.*)$ index.php [L]
    </IfModule>
    <IfModule !mod_rewrite.c>
        ErrorDocument 403 /error.php?code=403
    </IfModule>
    

    文件中应包含以下内容error.php:

    if (!function_exists('http_response_code_text'))
    {
        function http_response_code_text($code = 403)
        {
            $text = 'Forbidden';
            switch ($code)
            {
                case 400: $text = 'Bad Request'; break;
                case 401: $text = 'Unauthorized'; break;
                case 403: $text = 'Forbidden'; break;
                case 404: $text = 'Not Found'; break;
                case 500: $text = 'Internal Server Error'; break;
                default:
                    $text = 'Forbidden';
                break;
            }
            return $text;
        }
    }
    
    $code = 403;
    if(isset($_GET['code']{0}))
    {
        $code = (int) $_GET['code'];
    }
    
    $text = http_response_code_text($code);
    echo json_encode(array('httpStatusCode' => $code, 'httpStatusText' => $text));
    

    这似乎不适用于所有 HTTP 错误代码,例如 414“Request-URI Too Long”,并且总是回退到 403 Forbidden。

    • 1

相关问题

  • apache重写以将文件夹分配给域

  • Apache conf 或 .htaccess 规则将 404 重定向到特定文件类型的另一个位置

  • 如何让 Apache2 重定向到子目录

  • .htaccess - 删除所有 cookie

  • 我的 .htaccess 文件是什么意思?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve