<-
Apache > HTTP Server > Documentation > Version 2.3

Expressions in Apache HTTP Server

Available Languages:  en 

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.

top

Grammar in BNF notation

expr        ::= "true" | "false"
              | "!" expr
              | expr "&&" expr
              | expr "||" expr
              | "(" expr ")"
              | comp

comp        ::= stringcomp
              | integercomp
              | unaryop word
              | word binaryop word
              | word "in" "{" wordlist "}"
              | word "in" listfunction
              | word "=~" regex
              | word "!~" regex


stringcomp  ::= word "==" word
              | word "!=" word
              | word "<"  word
              | word "<=" word
              | word ">"  word
              | word ">=" word

integercomp ::= word "-eq" word | word "eq" word
              | word "-ne" word | word "ne" word
              | word "-lt" word | word "lt" word
              | word "-le" word | word "le" word
              | word "-gt" word | word "gt" word
              | word "-ge" word | word "ge" word

wordlist    ::= word
              | wordlist "," word

word        ::= word "." word
              | digit
              | "'" string "'"
              | """ string """
              | variable
              | rebackref
              | function

string      ::= stringpart
              | string stringpart

stringpart  ::= cstring
              | variable
              | rebackref

cstring     ::= ...
digit       ::= [0-9]+

variable    ::= "%{" varname "}"
              | "%{" funcname ":" funcargs "}"

rebackref   ::= "$" [0-9]

function     ::= funcname "(" word ")"

listfunction ::= listfuncname "(" word ")"
top

Variables

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 witht the req function.

Name
HTTP_ACCEPT
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_REFERER
HTTP_USER_AGENT

Other request related variables

NameDescription
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 URI of the request
REQUEST_FILENAME
SCRIPT_FILENAME Same as REQUEST_FILENAME
PATH_INFO
QUERY_STRING The query string of the current request
IS_SUBREQ "true" if the current request is a subrequest, "false" otherwise
THE_REQUEST The complete request line (e.g., "GET /index.html HTTP/1.1")
REMOTE_ADDR The IP address of the remote host
REMOTE_HOST The host name of the remote host
REMOTE_USER The name of the authenticated user (if any)
REMOTE_IDENT The user name set by mod_ident
SERVER_NAME The ServerName of the current vhost
SERVER_PORT The server port of the current vhost, see ServerName
SERVER_ADMIN The ServerAdmin of the current vhost
SERVER_PROTOCOL The protocol used by the request
DOCUMENT_ROOT The DocumentRoot of the current vhost
AUTH_TYPE The configured AuthType (e.g. "basic")
CONTENT_TYPE The content type of the response
HANDLER The name of the handler creating the response
HTTPS "on" if the request uses https, "off" otherwise
IPV6 "on" if the connection uses IPv6, "off" otherwise
REQUEST_LOG_ID The error log id of the request (see ErrorLogFormat)
CONN_LOG_ID The error log id of the connection (see ErrorLogFormat)

Misc variables

NameDescription
TIME_YEAR The current year (e.g. 2010)
TIME_MON The current month (1, ..., 12)
TIME_DAY The current day of the month
TIME_HOUR The hour part of the current time (0, ..., 23)
TIME_MIN The minute part of the current time
TIME_SEC The second part of the current time
TIME_WDAY The day of the week (starting with 0 for Sunday)
TIME The date and time in the format 20101231235959
SERVER_SOFTWARE The server version string
API_VERSION The date of the API version (module magic number)

Some modules register additional variables, see e.g. mod_ssl.

top

Binary operators

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.

Comparison operators

NameAlternative Description
== = String equality
!= String inequality
< String less than
<= String less than or equal
> String greater than
>= String greater than or equal
-eq eq Integer equality
-ne ne Integer inequality
-lt lt Integer less than
-le le Integer less than or equal
-gt gt Integer greater than
-ge ge Integer greater than or equal

Other binary operators

NameDescription
-ipmatch IP address matches address/netmask
-strmatch left string matches pattern given by right string (containing wildcards *, ?, [])
-strcmatch same as -strmatch, but case insensitive
-fnmatch same as -strmatch, but slashes are not matched by wildcards
top

Unary operators

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

NameDescription
-n String is not empty
-z String is empty
-R Same as "%{REMOTE_ADDR} -ipmatch ...", but more efficient
top

Functions

Normal string-valued functions take one string as argument and return a string. Functions names are not case sensitive. Modules may register additional functions.

NameDescription
req, http Get HTTP request header
resp Get HTTP response header
reqenv Lookup request environment variable
osenv Lookup operating system environment variable
note Lookup request note
env Return first match of note, reqenv, osenv
tolower Convert string to lower case
toupper Convert string to uppser case
escape Escape special characters in %hex encoding
unescape Unescape %hex encoded string, leaving URL-special characters encoded (XXX: describe better)
file Read contents from a file

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).

top

Other

NameAlternative Description
-in in string contained in string list
/regexp/ m#regexp# Regular expression (the second form allows different delimiters than /)
/regexp/i m#regexp#i Case insensitive regular expression
$0 ... $9 Regular expression backreferences

Regular expression backreferences

The strings $0 ... $9 allow to reference the capture groups form 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.

Available Languages:  en