gpt4 book ai didi

php - 如何在 PHP 中显示 Apache 默认的 404 页面

转载 作者:IT王子 更新时间:2023-10-28 23:52:44 24 4
gpt4 key购买 nike

我有一个 Web 应用程序需要处理 URI 以查找某个页面是否存在于数据库中。我可以使用 .htaccess 将 URI 定向到应用程序:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [NC]

我的问题是,如果页面不存在,我不想使用用 PHP 编写的自定义 404 处理程序,我想显示默认的 Apache 404 页面。有什么方法可以让 PHP 在确定页面不存在时将执行交还给 Apache?

最佳答案

我认为您不能将其“交还”给 Apache,但您可以发送适当的 HTTP header ,然后像这样显式包含您的 404 文件:

if (! $exists) {
header("HTTP/1.0 404 Not Found");
include_once("404.php");
exit;
}

更新

PHP 5.4 引入了 http_response_code使这更容易记住的功能。

if (! $exists) {
http_response_code(404);
include_once("404.php");
exit;
}

关于php - 如何在 PHP 中显示 Apache 默认的 404 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5953096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com