You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.htaccess访问不存在路径返回500而非404错误排查求助

.htaccess访问不存在路径返回500错误修复方案

问题根源

当前配置出现500错误是Apache重写规则递归次数触达上限导致,核心问题为两段php隐藏后缀的规则逻辑重叠:当访问/biography/test/这类不存在的路径时,规则会反复尝试为路径拼接.php后缀匹配文件,无限递归后触发服务器内部错误。

修复后的完整配置

Options -Indexes
AddDefaultCharset UTF-8
ServerSignature Off
DirectoryIndex index.php

RewriteEngine On
RewriteBase /

# HTTPS和域名强制跳转
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

# 非文件路径自动补尾斜杠
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !(^$|\.[a-zA-Z0-9]{1,5}|/)$ %{REQUEST_URI}/ [R=301,L]

# 隐藏.php后缀,同时避免递归
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]

# 仅当对应php文件存在时才执行重写
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]

验证效果

  • 访问biography.php会自动301跳转至/biography/
  • 访问/biography/可正常加载biography.php的内容
  • 访问/biography/test/等不存在的路径时,会正常返回404错误
  • 其他单层级php文件的隐藏后缀、跳转逻辑均正常生效

内容的提问来源于stack exchange,提问作者user17115092

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 16:18:01