Mod 重写正则表达式负前瞻

Mod Rewrite Regex Negative Lookahead

我正在尝试匹配所有以 #/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]

您还可以使用所有格量词将您的否定先行条​​件更改为此:

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