我正在尝试在一个重写文件中创建if块,如果URL模式匹配则调用不同的重写文件
<If "%{REQUEST_URI} =~ /category1(.*)$/">
Include conf.d/sites/abc/301_redirections_category1.conf
</If>
<ElseIf "%{REQUEST_URI} =~ /category2/subcategory1(.*)$/">
Include conf.d/sites/abc/301_redirections_category21.conf
</ElseIf>
<Else "%{REQUEST_URI} =~ /category2/subcategory2(.*)$/">
Include conf.d/sites/abc/301_redirections_category22.conf
</Else>
我面临的问题:
/category1(.*)$/
下,它允许任何包含category1的网址,而不是以/ category1开头的网址我尝试了不同的语法,但没有成功
m#/category1(.*)$/#i
m#/\s/category1(.*)$/#i
m#/\/category1(.*)$/#i
m#\s/category2\s/sucategory1(.*)$/#i
答案 0 :(得分:1)
您可以在正则表达式模式中添加开始锚点的情况下尝试以下代码吗?
<If "%{REQUEST_URI} =~ m#^/category1#">
Include conf.d/sites/abc/301_redirections_category1.conf
</If>
<ElseIf "%{REQUEST_URI} =~ m#^/category2/subcategory1#">
Include conf.d/sites/abc/301_redirections_category21.conf
</ElseIf>
<ElseIf "%{REQUEST_URI} =~ m#^/category2/subcategory2#">
Include conf.d/sites/abc/301_redirections_category22.conf
</ElseIf>