使用 RewriteCond 仅匹配具有相同名称的页面或目录
Using RewriteCond to match only a page or a directory having the same name
我正在尝试使用 .htaccess
重定向文件和文件夹。我想在 RewriteRule
之前添加一个 RewriteCond
指令以仅匹配 example.com/place/sta-ana.html
和 example.com/place/sta-ana
目录。我试过这个:
RewriteCond %{REQUEST_URI} ^place/sta-ana(\.html|/.*)?
RewriteRule ^place/sta-ana(\.html|/.*)? /place/santa-ana [L,NC,R=301]
还有这个:
RewriteCond %{THE_REQUEST} ^place/sta-ana(\.html|/.*)?
RewriteRule ^place/sta-ana(\.html|/.*)? /place/santa-ana [L,NC,R=301]
但两者都只适用于重定向 place/sta-ana.html
而不是目录及其文件,如 place/sta-ana/santiago.html
和 place/sta-ana/santiago/lourdes.html
。我如何获得上面的代码以将 sta-ana
目录也重定向到 santa-ana
文件夹:
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
看来您不需要 RewriteCond
。只需这条规则就可以完成工作:
RewriteRule ^place/sta-ana(\.html|/.*)$ /place/santa-ana [L,NC,R=301,NE]
确保这是最重要的规则,并且在测试此更改之前必须完全清除浏览器缓存。
我正在尝试使用 .htaccess
重定向文件和文件夹。我想在 RewriteRule
之前添加一个 RewriteCond
指令以仅匹配 example.com/place/sta-ana.html
和 example.com/place/sta-ana
目录。我试过这个:
RewriteCond %{REQUEST_URI} ^place/sta-ana(\.html|/.*)?
RewriteRule ^place/sta-ana(\.html|/.*)? /place/santa-ana [L,NC,R=301]
还有这个:
RewriteCond %{THE_REQUEST} ^place/sta-ana(\.html|/.*)?
RewriteRule ^place/sta-ana(\.html|/.*)? /place/santa-ana [L,NC,R=301]
但两者都只适用于重定向 place/sta-ana.html
而不是目录及其文件,如 place/sta-ana/santiago.html
和 place/sta-ana/santiago/lourdes.html
。我如何获得上面的代码以将 sta-ana
目录也重定向到 santa-ana
文件夹:
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
看来您不需要 RewriteCond
。只需这条规则就可以完成工作:
RewriteRule ^place/sta-ana(\.html|/.*)$ /place/santa-ana [L,NC,R=301,NE]
确保这是最重要的规则,并且在测试此更改之前必须完全清除浏览器缓存。