The If and ElseIf expressions may evaluate regular expressions using the =~ and !~ regular expression matching operators. These regular expressions use the Perl-compatible syntax implemented by Perl-compatible Regular Expressions (PCRE).
By default, regular expressions are case sensitive. The (?i) option flag can be added to the beginning of a regular expression to request case insensitivity. For example:
$uri =~ '^/[Ff][Ii][Ll][Ee][Nn][Aa][Mm][Ee]$' $uri =~ '(?i)^/filename$'
When an If or ElseIf expression contains a regular expression, regular expression backreferences can appear within arguments in the container body. Regular expression backreferences are of the form $n where n is an integer between 1 and 9 corresponding to the capturing subpattern of the regular expression.
<If $path =~ '^(.*)(\.html|\.htm)$'> NameTrans fn="rewrite" path="$1.shtml" </If>
In the above example, two subpatterns are used in the If expression, so $1 and $2 can be used as backreferences. In the example, the value of the first capturing subpattern is used within a NameTrans fn="rewrite" parameter. The value of the second capturing subpattern is ignored.
An If or ElseIf expression can contain backreferences to earlier regular expressions in that same If or ElseIf expression.
<If "foo" =~ "(.*)" and $1 eq "foo"> # Any contained directives will be executed # since $1 will evaluate to "foo" ... </If>
The contents of the above If expression are executed, because the given If expression always evaluates to true.
However, If and Elseif expressions, and contained directives, can't contain backreferences to regular expressions in parent containers. For example, the following obj.conf entry is invalid:
<If $path =~ '(.*)\.css'> <If $browser = "*MSIE*"> # This example is invalid as $1 is not defined AuthTrans fn="rewrite" path="$1-msie.css" </If> </If>
You can use $& to obtain the value that last successfully matched a regular expression. Use the following obj.conf entry to redirect requests for HTML files to another server:
<If $path =~ '\.html$' or $path =~ '\.htm$' > NameTrans fn="redirect" url="http://docs.example.com$&" </If>