.htaccess及Apache&Nginx配置下自定义404错误页无法正常工作
自定义404错误页仅对.html不存在路径生效,其他情况显示默认“File not found”
我给项目做了自定义404错误页,但只有访问不存在的.html路径时能正常显示,其他情况都只显示空白页和“File not found”提示:
- 访问
www.example.com/wrongurl(无后缀不存在路径):显示空白页及“File not found” - 访问
www.example.com/wrongurl.php(不存在的php路径):显示空白页及“File not found” - 访问
www.example.com/wrongurl.html(不存在的html路径):正常加载自定义404页
我已经通过.htaccess文件和Plesk面板的Apache & Nginx设置配置过自定义404,具体如下:
当前.htaccess内容
Options -MultiViews RewriteEngine On RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule ^ - [L] # Append trailing slash if omitted (canonical redirect) RewriteRule ^news$ /$0/ [R=301,L] RewriteCond %{QUERY_STRING} (?:^|&)article_slug=([\w-]*)(?:$|&) RewriteRule ^news\.php$ /news/%1 [R=301,L] RewriteRule ^news/([\w-]*)$ /news.php?article_slug=$1 [QSA,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] ErrorDocument 404 /404.php
服务器配置
Plesk面板的Apache & Nginx设置中已配置自定义404错误页指向/404.php
已排查的操作
- 验证
.htaccess正常解析:添加无效行“foo bar”后访问不存在页面,触发500内部服务器错误,确认文件被处理 - 确认
/404.php的路径、文件权限均正确,直接访问该URL可正常加载内容 - 确认Apache全局配置允许使用自定义错误页
- 尝试将ErrorDocument指令改为绝对路径(如
ErrorDocument 404 /var/www/html/404.php),问题依旧
求各位帮忙解决这个问题。
内容的提问来源于stack exchange,提问作者Nashie
相关产品推荐
相关产品推荐

