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

如何使用RewriteCond匹配同名页面与目录实现.htaccess重定向

规则错误原因

  1. 第一段代码中%{REQUEST_URI}变量的值始终以/开头,你写的正则^place/没有匹配开头的斜杠,导致目录路径匹配失效。
  2. 第二段代码中%{THE_REQUEST}存储的是完整原始请求行(格式为GET /place/sta-ana.html HTTP/1.1),开头为请求方法,不可能匹配^place/开头的规则,写法本身不成立。

正确规则

RewriteCond %{REQUEST_URI} ^/place/sta-ana(\.html|/.*)?$ [NC]
RewriteRule ^place/sta-ana(\.html|/.*)?$ /place/santa-ana$1 [L,NC,R=301]

测试注意事项

修改规则后先清空浏览器缓存再测试,301为永久重定向,浏览器会默认缓存旧的重定向规则,容易导致新规则不生效的假象。
以上规则可以完全覆盖你需要的重定向效果:

  • example.com/place/sta-ana.html => example.com/place/santa-ana.html
  • example.com/place/sta-ana/santiago.html => example.com/place/santa-ana/santiago.html
  • example.com/place/sta-ana/santiago/lourdes.html => example.com/place/santa-ana/santiago/lourdes.html

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 06:18:03