You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Michael Chen <mi...@rivetlogic.com> on 2019/07/24 19:14:08 UTC

Negative pattern match in rewrite rule is not as documented

Hi,

The mention of using "NOT character ('!')" in rule patterns as negative
match no longer matches implementation:

https://tomcat.apache.org/tomcat-8.5-doc/rewrite.html

In the rules, the NOT character ('!') is also available as a possible
> pattern prefix. This enables you to negate a pattern; to say, for instance:
> ``*if the current URL does NOT match this pattern*''. This can be used
> for exceptional cases, where it is easier to match the negative pattern, or
> as a last default rule.
>

The current implementation uses java.util.regex, which does not support a
simple ! prefix as a negative match regex:

https://docs.oracle.com/javase/8/docs/api/index.html?java/util/regex/Pattern.html

Instead, you need to use zero-width lookahead like this (to match any URL
but /portal/api/.*)

^(?!/portal/api/.*).*$

This inaccurate documentation exist in all versions, at least since 8.0.x.
For backward compatibility to the above documentation, RewriteRule needs
the `positive` variable and logic found in the RewriteCond class.

https://bz.apache.org/bugzilla/show_bug.cgi?id=63608

Thanks

--Michael
.

-- 
CONFIDENTIALITY NOTICE: This e-mail, including attachments, is for the sole 
use of the intended recipient(s) and may contain confidential and 
privileged information or otherwise be protected by law. Any unauthorized 
review, use, disclosure or distribution is prohibited. If you are not the 
intended recipient, please contact the sender and destroy all copies and 
the original message.

Re: Negative pattern match in rewrite rule is not as documented

Posted by Mark Thomas <ma...@apache.org>.
On 24/07/2019 20:14, Michael Chen wrote:
> Hi,
> 
> The mention of using "NOT character ('!')" in rule patterns as negative
> match no longer matches implementation:
> 
> https://tomcat.apache.org/tomcat-8.5-doc/rewrite.html
> 
> In the rules, the NOT character ('!') is also available as a possible
>> pattern prefix. This enables you to negate a pattern; to say, for instance:
>> ``*if the current URL does NOT match this pattern*''. This can be used
>> for exceptional cases, where it is easier to match the negative pattern, or
>> as a last default rule.
>>
> 
> The current implementation uses java.util.regex, which does not support a
> simple ! prefix as a negative match regex:
> 
> https://docs.oracle.com/javase/8/docs/api/index.html?java/util/regex/Pattern.html
> 
> Instead, you need to use zero-width lookahead like this (to match any URL
> but /portal/api/.*)
> 
> ^(?!/portal/api/.*).*$
> 
> This inaccurate documentation exist in all versions, at least since 8.0.x.
> For backward compatibility to the above documentation, RewriteRule needs
> the `positive` variable and logic found in the RewriteCond class.
> 
> https://bz.apache.org/bugzilla/show_bug.cgi?id=63608

Thanks. I see the bug report. This is next on my TODO list. Although if
someone wants to beat me to it with a patch that would be great :)

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org