【问题标题】:Mod Rewrite Regex Negative LookaheadMod重写正则表达式负前瞻
【发布时间】:2017-08-26 05:52:26
【问题描述】:

我正在尝试匹配所有以 #/tool_[a-z\-]+# 开头的 URI,除了,如果它后跟 /public。比如/tool_calculator或者其他什么的。

例如,如果 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]

【问题讨论】:

    标签: regex .htaccess mod-rewrite regex-greedy negative-lookahead


    【解决方案1】:

    您还可以使用占有量词将您的负前瞻条件更改为此:

    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.+) true。

    【讨论】:

    • 成功了。谢谢[a-z]+的呢?我没完全听懂:'(
    • See this demo to understand better 检查regex debugger 链接以更好地了解回溯部分。一旦明确将正则表达式更改为^/tool_[a-z-]++(?!/public),现在将没有匹配项并再次检查regex debugger
    猜你喜欢
    • 2021-10-11
    • 2011-10-14
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多