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

.htaccess正则规则需匹配至少一个字符而非空字符的问题

解决.htaccess Rewrite规则误匹配根域名的问题

你当前的规则RewriteRule ^([a-z0-9-]+)?$ page.php?id=$1 [NC,QSA]里,捕获组后的?表示该组可选匹配,所以访问根域名(空路径)时规则依然会触发,导致跳转到page.php?id=。

修改方法很简单,去掉捕获组末尾的?,确保只有路径中存在至少一个符合[a-z0-9-]规则的字符时才触发重写:

RewriteRule ^([a-z0-9-]+)$ page.php?id=$1 [NC,QSA]

如果需要兼容带斜杠的路径(比如https://example.com/blahblah/),可以在规则末尾添加可选的斜杠匹配:

RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,QSA]

修改后效果:

  • https://example.com/blahblah 和 https://example.com/blahblah/ 都会正确跳转到 https://example.com/page.php?id=blahblah
  • 访问根域名https://example.com时不会触发该规则,避免空id的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 13:42:35