自定义 404 显示 "File not found" php 个文件而不是重定向

Custom 404 shows "File not found" for php files instead of redirecting

我正在尝试创建自定义 404 错误页面。

到目前为止,我所拥有的功能可以很好地处理所有不存在的文档、文件夹、图像、htmls 等 - 打开我的自定义 404 页面(输出 Custom 404!)

但是,当我尝试访问一个 .php 扩展名不存在的文件时,我得到了这个:

...在屏幕上显示“找不到文件。”,而不是被重定向到我的 404 文档。

我的.htaccess

RewriteEngine On
ErrorDocument 404 /404.php

我的404.php

<?php

header("HTTP/1.0 404 Not Found");
include '404.html';
die();

?>

我的404.html

<!DOCTYPE html>
<html>
<head>
<title>Custom 404 | Not found!</title>
</head>
<body>

Custom 404!

</body>
</html>

编辑

为了更清楚这里发生了什么。

我相信路径和 .htaccess 工作得很好,但 php 必须有一些技巧。这里有几个例子:

当我尝试打开我的服务器上不存在的一些文件时:

我怀疑不同之处在于 php 文件启动解释器,而 html 文件只是提供服务。一种可行但可能不是唯一或最好的解决方案是将此指令添加到主机配置文件中:

ProxyErrorOverride on

发生这种情况是因为您可能启用了 mod_proxy_fcgi 并且您正在使用 PHP-FPM。代理中的默认错误页面是 File not found。因此,将以下内容添加到您的 httpd.conf 或 apache2.conf,而不是您的 .htaccess。

它使您的 .htaccess 文件的 ErrorDocument 指令覆盖默认代理错误页面。

ProxyErrorOverride On

谢谢。