最近我为我的服务器设置了定制的错误文档。
我从一个 404 页面开始,这很有用:找不到文件会自动显示指定的 404.php 页面。 但是,对于 403,我遇到了一些麻烦。我以同样的方式设置它,但我只得到一个空白页。它不显示 .htaccess 文档中设置的 403.php 页面。 有什么想法吗?
这是我的代码:
.htaccess:
Options -Indexes
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
PHP:
<?php
header("HTTP/1.1 403 Unauthorized");
exit;
?>
最佳答案
服务器配置中定义的错误文档仅在服务器 (Apache) 遇到 403 错误时加载。
如果您通过 PHP 强制执行错误,例如发送 403 状态代码,这种情况发生在 PHP 而不是 Apache 中。
因此,当您已经知道我要呈现的网站将因未经授权而失败时,为什么还要让 apache 来决定会发生什么?
<?php
header("HTTP/1.1 403 Unauthorized");
// either:
header("Location: /403.php");
// or:
include('403.php');
exit;
?>
I started with a 404 page and this works like a charm:
你确定吗?
你有脚本发送 404 并重定向到 404.php 吗? 还是您只是打开了一个不存在的 URI 并被重定向?
关于php - 在 php 中触发 403,以便它显示 ErrorDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21287328/