Apache regular expression operator ~= seems to fail

Viewed 191

I am editing the Apache conf for my site with If directive to set some headers (for example) but the string to match regular expression operator gives syntax issue.

For example:

<If "%{HTTP_USER_AGENT} =~ RBTools.*">
       Header set test_header %{HTTP_USER_AGENT}e
       Require all granted
       MellonEnable "off"
</If>

gives syntax error on the If statement. I have tried to enclose it in many other ways like so:

<If "'%{HTTP_USER_AGENT}' =~ RBTools.*">
<If "%{HTTP_USER_AGENT} =~ 'RBTools.*'">
<If "'%{HTTP_USER_AGENT}' =~ 'RBTools.*'">
<If %{HTTP_USER_AGENT}' "=~" RBTools.*>
<If "%{HTTP_USER_AGENT}' '=~' RBTools.*">

with all the same syntax issue.

The expression is documented here: https://httpd.apache.org/docs/current/expr.html

What should be the correct syntax? The syntax doesn't complain when I use the string equality comparitor "=="

1 Answers

Turns out you needed to have a slash before the regular expression like:

<If "%{HTTP_USER_AGENT} =~ /RBTools/">

I tried to do

# FAILS
<If "%{HTTP_USER_AGENT} =~ Chrome/">
# SUCCEEDS
<If "%{HTTP_USER_AGENT} =~ /Chrome/">

which gave me the hint to do the same.

Related