如何使用RewriteCond匹配同名页面与目录实现.htaccess重定向
规则错误原因
- 第一段代码中
%{REQUEST_URI}变量的值始终以/开头,你写的正则^place/没有匹配开头的斜杠,导致目录路径匹配失效。 - 第二段代码中
%{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.htmlexample.com/place/sta-ana/santiago.html=>example.com/place/santa-ana/santiago.htmlexample.com/place/sta-ana/santiago/lourdes.html=>example.com/place/santa-ana/santiago/lourdes.html
内容的提问来源于stack exchange,提问作者JAT86
相关产品推荐
相关产品推荐

