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

.htaccess rewrite规则在doctor子目录下无法生效问题排查

问题根因

你当前的重写规则使用^subcategory/开头匹配,默认仅匹配规则生效目录下路径开头为subcategory/的请求:

  • 当请求路径为http://localhost:8888/community/subcategory/health时,/community是规则生效目录,传递给规则匹配的相对路径为subcategory/health,刚好命中规则,正常转发到同目录下的child-category.php。
  • 当请求路径为http://localhost:8888/community/doctor/subcategory/health时,传递给规则匹配的相对路径为doctor/subcategory/health,开头不是subcategory/,无法命中对应规则,会走到后续的.php后缀补全规则,尝试访问不存在的doctor/subcategory/health.php,因此访问失效。
修复方案

根据你的需求选择对应配置即可:

方案1:适配任意子目录下的subcategory请求

如果希望/任意层级目录/subcategory/xxx自动转发到对应目录下的child-category.php(即/community/doctor/subcategory/health转发到/community/doctor/child-category.php,/community/subcategory/health保持原有逻辑),修改规则如下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# 新增前置目录匹配逻辑
RewriteRule ^(.*/)?subcategory/([0-9a-zA-Z_-]+) $1child-category.php?cat=$2 [NC,L,QSA]
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]

规则说明:(.*/)?会匹配0层或多层前置目录,匹配到的目录路径会存入$1变量,拼接后自动指向对应目录下的child-category.php文件。

方案2:所有subcategory请求统一转发到doctor目录下的文件

如果希望所有subcategory/xxx请求都指向/community/doctor/child-category.php,修改规则如下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# 同时匹配根目录和doctor目录下的subcategory路径
RewriteRule ^(doctor/)?subcategory/([0-9a-zA-Z_-]+) doctor/child-category.php?cat=$2 [NC,L,QSA]
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 18:09:03