apache/.htaccess:不正确的页面不会重定向到自定义 404

apache/.htaccess: incorrect page does not redirect to custom 404

我有一个奇怪的错误,我在 .htaccess 中的 ErrorDocument 404 在所有情况下都无法正常工作。

如果我访问一个不存在的页面,我只会收到消息 File not found.

如果我添加了 .html 扩展,它会显示我创建的自定义 404 页面。

https://website.com/not-found

这个 link 不起作用,我只是收到消息 File not found.

https://website.com/not-found.html

但是,访问此 link 时,我看到了我的自定义 404 页面。

这是我的 .htaccess 文件

<IfModule mod_rewrite.c>

    Options +FollowSymLinks -MultiViews
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+?)/?$ .php [NC,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ / [R=301,L]

    ErrorDocument 404 /app/templates/errors/404.php
    ErrorDocument 403 /app/templates/errors/403.php

    # Block access to hidden files and directories.
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]

</IfModule>

基本上我去掉了扩展名,仅此而已。在其他网站上,此代码运行良好,可能是托管公司的问题吗? 请注意,我无权访问 apache 配置文件

谢谢

像这样更改您的第一个文件以检查 .php 文件是否存在:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ .php [L]

在没有 RewriteCond %{REQUEST_FILENAME}.php -f 的情况下,它会重写每个非文件和非目录。