Expressions in Apache HTTP Server

Historically, there are several syntax variants for expressions
used to express a condition in the different modules of the Apache
HTTP Server. There is some ongoing effort to only use a single
variant, called ap_expr, for all configuration directives.
This document describes the ap_expr expression parser.

The ap_expr expression is intended to replace most other
expression variants in HTTPD. For example, the deprecated SSLRequire expressions can be replaced
by Require expr.

Backus-Naur
Form (BNF) is a notation technique for context-free grammars,
often used to describe the syntax of languages used in computing.
In most cases, expressions are used to express boolean values.
For these, the starting point in the BNF is expr.
However, a few directives like LogMessage accept expressions
that evaluate to a string value. For those, the starting point in
the BNF is string.

The expression parser provides a number of variables of the form
%{HTTP_HOST}. Note that the value of a variable may depend
on the phase of the request processing in which it is evaluated. For
example, an expression used in an <If >
directive is evaluated before authentication is done. Therefore,
%{REMOTE_USER} will not be set in this case.

The following variables provide the values of the named HTTP request
headers. The values of other headers can be obtained with the
reqfunction. Using these
variables may cause the header name to be added to the Vary
header of the HTTP response, except where otherwise noted for the
directive accepting the expression. The req_novaryfunction may be used to circumvent this
behavior.

Name

HTTP_ACCEPT

HTTP_COOKIE

HTTP_FORWARDED

HTTP_HOST

HTTP_PROXY_CONNECTION

HTTP_REFERER

HTTP_USER_AGENT

Other request related variables

Name

Description

REQUEST_METHOD

The HTTP method of the incoming request (e.g.
GET)

REQUEST_SCHEME

The scheme part of the request's URI

REQUEST_URI

The path part of the request's URI

DOCUMENT_URI

Same as REQUEST_URI

REQUEST_FILENAME

The full local filesystem path to the file or script matching the
request, if this has already been determined by the server at the
time REQUEST_FILENAME is referenced. Otherwise, such
as when used in virtual host context, the same value as
REQUEST_URI

SCRIPT_FILENAME

Same as REQUEST_FILENAME

LAST_MODIFIED

The date and time of last modification of the file in the format
20101231235959, if this has already been determined by
the server at the time LAST_MODIFIED is referenced.

With the exception of some built-in comparison operators, binary
operators have the form "-[a-zA-Z][a-zA-Z0-9_]+", i.e. a
minus and at least two characters. The name is not case sensitive.
Modules may register additional binary operators.

Unary operators take one argument and have the form
"-[a-zA-Z]", i.e. a minus and one character.
The name is case sensitive.
Modules may register additional unary operators.

Name

Description

Restricted

-d

The argument is treated as a filename.
True if the file exists and is a directory

yes

-e

The argument is treated as a filename.
True if the file (or dir or special) exists

yes

-f

The argument is treated as a filename.
True if the file exists and is regular file

yes

-s

The argument is treated as a filename.
True if the file exists and is not empty

yes

-L

The argument is treated as a filename.
True if the file exists and is symlink

yes

-h

The argument is treated as a filename.
True if the file exists and is symlink
(same as -L)

yes

-F

True if string is a valid file, accessible via all the server's
currently-configured access controls for that path. This uses an
internal subrequest to do the check, so use it with care - it can
impact your server's performance!

-U

True if string is a valid URL, accessible via all the server's
currently-configured access controls for that path. This uses an
internal subrequest to do the check, so use it with care - it can
impact your server's performance!

Hash the string using MD5, then encode the hash with hexadecimal
encoding

sha1

Hash the string using SHA1, then encode the hash with hexadecimal
encoding

file

Read contents from a file (including line endings, when present)

restricted

filemod

Return last modification time of a file (or 0 if file does not exist
or is not regular file)

restricted

filesize

Return size of a file (or 0 if file does not exist or is not
regular file)

restricted

The functions marked as "restricted" in the final column are not
available in some modules like mod_include.

The functions marked as "ordering" in the final column require some
consideration for the ordering of different components of the server,
especially when the function is used within the
<If> directive which is
evaluated relatively early.

Environment variable ordering

When environment variables are looked up within an
<If> condition, it's important
to consider how extremely early in request processing that this
resolution occurs. As a guideline, any directive defined outside of virtual host
context (directory, location, htaccess) is not likely to have yet had a
chance to execute. SetEnvIf
in virtual host scope is one directive that runs prior to this resolution

When reqenv is used outside of <If>, the resolution will generally occur later, but the
exact timing depends on the directive the expression has been used within.

When the functions req or http are used,
the header name will automatically be added to the Vary header of the
HTTP response, except where otherwise noted for the directive accepting
the expression. The req_novary function can be used to
prevent names from being added to the Vary header.

In addition to string-valued functions, there are also
list-valued functions which take one string as argument and return a
wordlist, i.e. a list of strings. The wordlist can be used with the
special -in operator. Functions names are not case
sensitive. Modules may register additional functions.

There are no built-in list-valued functions. mod_ssl
provides PeerExtList. See the description of
SSLRequire for details
(but PeerExtList is also usable outside
of SSLRequire).

The strings $0 ... $9 allow to reference
the capture groups from a previously executed, successfully
matching regular expressions. They can normally only be used in the
same expression as the matching regex, but some modules allow special
uses.

Notice:This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our mailing lists.