我有一个文件 secret.php
,它包含在 index.php
中,我想在有人尝试访问 secret 时显示 404 错误页面。 php
直接。但是
header("Location: this_site_certainly_does_not_exist");
in secure.php
不是解决方案,因为我希望用户键入的 URL(例如 example.com/secret.php
)在浏览器的显示错误页面时的 URL 栏,而不是重定向。
最佳答案
你可以用标题来做到这一点。
header("HTTP/1.0 404 Not Found");
然后您可以使用例如 readfile
方法添加您的 404 页面。
header("HTTP/1.0 404 Not Found");
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();
关于php - 显示来自 PHP 文件的 404 错误页面,无需重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20300789/