Mod Rewrite Regex Negative Lookahead

问题描述:

我试图匹配所有以#/tool_[a-z\-]+#开头的URI,除了,如果它跟着/public。如/tool_calculator或其他。Mod Rewrite Regex Negative Lookahead

例如,如果URI以/tool_store-front/tool_store-front/anything-but-public开头,那么我想将它们重定向到HTTPS。所以,/tool_store-front/public不是重定向。

这里是我已经和它不工作

RewriteCond %{HTTPS} =off 
RewriteCond %{REQUEST_URI} ^/?tool_[a-z-]+(?!/public.+) [OR] 
RewriteCond %{REQUEST_URI} ^/?secure 
RewriteCond %{REQUEST_URI} !^/?secure/public/info 
RewriteRule ^(.*)$ https://www.example.org%{REQUEST_URI} [NC,L] 

您也可以使用占有欲量词改变你的消极先行条件是:

RewriteCond %{HTTPS} =off 
RewriteCond %{REQUEST_URI} ^/tool_[a-z-]++(?!/public) [NC,OR] 
RewriteCond %{REQUEST_URI} ^/secure(?!/public/info) [NC] 
RewriteRule^https://www.example.org%{REQUEST_URI} [NE,R=301,L] 

您也可以使用这种消极前瞻:

RewriteCond %{REQUEST_URI} ^/tool_[a-z-]+(?!.*/public) [NC,OR] 

问题在您的情况^/?tool_[a-z-]+(?!/public.+)是正则表达式引擎是回溯[a-z]+,在/之前的一个位置,以断言负面看(?!/public.+)真实。

+0

它工作。谢谢

+1

[看到这个演示更好地理解](https://regex101.com/r/Zipw5q/1)检查'正则表达式调试器'链接,以更好地理解回溯部分。一旦明确改变正则表达式到'^/tool_ [az - ] ++(?!/ public)',现在不会有匹配,再次检查'regex调试器' – anubhava