1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "./style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="./style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="expr.xml.meta">
25 <title>Expressions in Apache HTTP Server</title>
28 <p>Historically, there are several syntax variants for expressions used to express
29 a condition in the different modules of the Apache HTTP Server.
30 There is some ongoing effort to only use a single variant, called <em>ap_expr</em>,
31 for all configuration directives.
32 This document describes the <em>ap_expr</em> expression parser.
34 <p>The <em>ap_expr</em> expression is intended to replace most other
35 expression variants in HTTPD. For example, the deprecated
36 <directive module="mod_ssl">SSLRequire</directive> expressions can be
37 replaced by <a href="mod/mod_authz_core.html#reqexpr">Require expr</a>.
41 <seealso><directive module="core" type="section">If</directive></seealso>
42 <seealso><directive module="core" type="section">ElseIf</directive></seealso>
43 <seealso><directive module="core" type="section">Else</directive></seealso>
44 <seealso><directive module="mod_rewrite">RewriteCond</directive></seealso>
45 <seealso><directive module="mod_setenvif">SetEnvIfExpr</directive></seealso>
46 <seealso><directive module="mod_headers">Header</directive></seealso>
47 <seealso><directive module="mod_headers">RequestHeader</directive></seealso>
48 <seealso><directive module="mod_filter">FilterProvider</directive></seealso>
49 <seealso><a href="mod/mod_authz_core.html#reqexpr">Require expr</a></seealso>
50 <seealso><directive module="mod_ssl">SSLRequire</directive></seealso>
51 <seealso><directive module="mod_log_debug">LogMessage</directive></seealso>
52 <seealso><module>mod_include</module></seealso>
54 <section id="grammar">
55 <title>Grammar in Backus-Naur Form notation</title>
56 <p><a href="http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form">Backus-Naur Form</a> (BNF) is a notation
57 technique for context-free grammars, often used to describe the syntax of languages used in computing.
58 In most cases, expressions are used to express boolean values.
59 For these, the starting boint in the BNF is <code>expr</code>. However, a few directives
60 like <directive module="mod_log_debug">LogMessage</directive> accept expressions
61 that evaluate to a string value. For those, the starting boint in the BNF is <code>code</code>.
65 expr ::= "<strong>true</strong>" | "<strong>false</strong>"
66 | "<strong>!</strong>" expr
67 | expr "<strong>&&</strong>" expr
68 | expr "<strong>||</strong>" expr
69 | "<strong>(</strong>" expr "<strong>)</strong>"
76 | word "<strong>in</strong>" "<strong>{</strong>" wordlist "<strong>}</strong>"
77 | word "<strong>in</strong>" listfunction
78 | word "<strong>=~</strong>" regex
79 | word "<strong>!~</strong>" regex
82 stringcomp ::= word "<strong>==</strong>" word
83 | word "<strong>!=</strong>" word
84 | word "<strong><</strong>" word
85 | word "<strong><=</strong>" word
86 | word "<strong>></strong>" word
87 | word "<strong>>=</strong>" word
89 integercomp ::= word "<strong>-eq</strong>" word | word "<strong>eq</strong>" word
90 | word "<strong>-ne</strong>" word | word "<strong>ne</strong>" word
91 | word "<strong>-lt</strong>" word | word "<strong>lt</strong>" word
92 | word "<strong>-le</strong>" word | word "<strong>le</strong>" word
93 | word "<strong>-gt</strong>" word | word "<strong>gt</strong>" word
94 | word "<strong>-ge</strong>" word | word "<strong>ge</strong>" word
97 | wordlist "<strong>,</strong>" word
99 word ::= word "<strong>.</strong>" word
101 | "<strong>'</strong>" string "<strong>'</strong>"
102 | "<strong>"</strong>" string "<strong>"</strong>"
107 string ::= stringpart
110 stringpart ::= cstring
117 variable ::= "<strong>%{</strong>" varname "<strong>}</strong>"
118 | "<strong>%{</strong>" funcname "<strong>:</strong>" funcargs "<strong>}</strong>"
120 rebackref ::= "<strong>$</strong>" [0-9]
122 function ::= funcname "<strong>(</strong>" word "<strong>)</strong>"
124 listfunction ::= listfuncname "<strong>(</strong>" word "<strong>)</strong>"
131 <title>Variables</title>
133 <p>The expression parser provides a number of variables of the form
134 <code>%{HTTP_HOST}</code>. Note that the value of a variable may depend
135 on the phase of the request processing in which it is evaluated. For
136 example, an expression used in an <directive><If ></directive>
137 directive is evaluated before authentication is done. Therefore,
138 <code>%{REMOTE_USER}</code> will not be set in this case.</p>
140 <p>The following variables provide the values of the named HTTP request
141 headers. The values of other headers can be obtained with the
142 <code>req</code> <a href="#functions">function</a>. Using these
143 variables may cause the header name to be added to the Vary
144 header of the HTTP response, except where otherwise noted for the
145 directive accepting the expression. The <code>req_novary</code>
146 <a href="#functions">function</a> may be used to circumvent this
149 <table border="1" style="zebra">
150 <columnspec><column width="1"/></columnspec>
152 <tr><th>Name</th></tr>
153 <tr><td><code>HTTP_ACCEPT</code></td></tr>
154 <tr><td><code>HTTP_FORWARDED</code></td></tr>
155 <tr><td><code>HTTP_HOST</code></td></tr>
156 <tr><td><code>HTTP_PROXY_CONNECTION</code></td></tr>
157 <tr><td><code>HTTP_REFERER</code></td></tr>
158 <tr><td><code>HTTP_USER_AGENT</code></td></tr>
162 <p>Other request related variables</p>
164 <table border="1" style="zebra">
165 <columnspec><column width=".4"/><column width=".6"/></columnspec>
167 <tr><th>Name</th><th>Description</th></tr>
168 <tr><td><code>REQUEST_METHOD</code></td>
169 <td>The HTTP method of the incoming request (e.g.
170 <code>GET</code>)</td></tr>
171 <tr><td><code>REQUEST_SCHEME</code></td>
172 <td>The scheme part of the request's URI</td></tr>
173 <tr><td><code>REQUEST_URI</code></td>
174 <td>The path part of the request's URI</td></tr>
175 <tr><td><code>DOCUMENT_URI</code></td>
176 <td>Same as REQUEST_URI</td></tr>
177 <tr><td><code>REQUEST_FILENAME</code></td>
178 <td>The full local filesystem path to the file or script matching the
179 request, if this has already been determined by the server at the
180 time <code>REQUEST_FILENAME</code> is referenced. Otherwise, such
181 as when used in virtual host context, the same value as
182 <code>REQUEST_URI</code> </td></tr>
183 <tr><td><code>SCRIPT_FILENAME</code></td>
184 <td>Same as <code>REQUEST_FILENAME</code></td></tr>
185 <tr><td><code>LAST_MODIFIED</code></td>
186 <td>The date and time of last modification of the file in the format
187 <code>20101231235959</code>, if this has already been determined by
188 the server at the time <code>LAST_MODIFIED</code> is referenced.
190 <tr><td><code>SCRIPT_USER</code></td>
191 <td>The user name of the owner of the script.</td></tr>
192 <tr><td><code>SCRIPT_GROUP</code></td>
193 <td>The group name of the group of the script.</td></tr>
194 <tr><td><code>PATH_INFO</code></td>
195 <td>The trailing path name information, see
196 <directive module="core">AcceptPathInfo</directive></td></tr>
197 <tr><td><code>QUERY_STRING</code></td>
198 <td>The query string of the current request</td></tr>
199 <tr><td><code>IS_SUBREQ</code></td>
200 <td>"<code>true</code>" if the current request is a subrequest,
201 "<code>false</code>" otherwise</td></tr>
202 <tr><td><code>THE_REQUEST</code></td>
203 <td>The complete request line (e.g.,
204 "<code>GET /index.html HTTP/1.1</code>")</td></tr>
205 <tr><td><code>REMOTE_ADDR</code></td>
206 <td>The IP address of the remote host</td></tr>
207 <tr><td><code>REMOTE_HOST</code></td>
208 <td>The host name of the remote host</td></tr>
209 <tr><td><code>REMOTE_USER</code></td>
210 <td>The name of the authenticated user (if any)</td></tr>
211 <tr><td><code>REMOTE_IDENT</code></td>
212 <td>The user name set by <module>mod_ident</module></td></tr>
213 <tr><td><code>SERVER_NAME</code></td>
214 <td>The <directive module="core">ServerName</directive> of
215 the current vhost</td></tr>
216 <tr><td><code>SERVER_PORT</code></td>
217 <td>The server port of the current vhost, see
218 <directive module="core">ServerName</directive></td></tr>
219 <tr><td><code>SERVER_ADMIN</code></td>
220 <td>The <directive module="core">ServerAdmin</directive> of
221 the current vhost</td></tr>
222 <tr><td><code>SERVER_PROTOCOL</code></td>
223 <td>The protocol used by the request</td></tr>
224 <tr><td><code>DOCUMENT_ROOT</code></td>
225 <td>The <directive module="core">DocumentRoot</directive> of
226 the current vhost</td></tr>
227 <tr><td><code>AUTH_TYPE</code></td>
228 <td>The configured <directive module="mod_authn_core">AuthType</directive>
229 (e.g. "<code>basic</code>")</td></tr>
230 <tr><td><code>CONTENT_TYPE</code></td>
231 <td>The content type of the response</td></tr>
232 <tr><td><code>HANDLER</code></td>
233 <td>The name of the <a href="handler.html">handler</a> creating
234 the response</td></tr>
235 <tr><td><code>HTTPS</code></td>
236 <td>"<code>on</code>" if the request uses https,
237 "<code>off</code>" otherwise</td></tr>
238 <tr><td><code>IPV6</code></td>
239 <td>"<code>on</code>" if the connection uses IPv6,
240 "<code>off</code>" otherwise</td></tr>
241 <tr><td><code>REQUEST_STATUS</code></td>
242 <td>The HTTP error status of the request</td></tr>
243 <tr><td><code>REQUEST_LOG_ID</code></td>
244 <td>The error log id of the request (see
245 <directive module="core">ErrorLogFormat</directive>)</td></tr>
246 <tr><td><code>CONN_LOG_ID</code></td>
247 <td>The error log id of the connection (see
248 <directive module="core">ErrorLogFormat</directive>)</td></tr>
249 <tr><td><code>CONN_REMOTE_ADDR</code></td>
250 <td>The peer IP address of the connection (see the
251 <module>mod_remoteip</module> module)</td></tr>
255 <p>Misc variables</p>
257 <table border="1" style="zebra">
258 <columnspec><column width=".4"/><column width=".6"/></columnspec>
260 <tr><th>Name</th><th>Description</th></tr>
261 <tr><td><code>TIME_YEAR</code></td>
262 <td>The current year (e.g. <code>2010</code>)</td></tr>
263 <tr><td><code>TIME_MON</code></td>
264 <td>The current month (<code>1</code>, ..., <code>12</code>)</td></tr>
265 <tr><td><code>TIME_DAY</code></td>
266 <td>The current day of the month</td></tr>
267 <tr><td><code>TIME_HOUR</code></td>
268 <td>The hour part of the current time
269 (<code>0</code>, ..., <code>23</code>)</td></tr>
270 <tr><td><code>TIME_MIN</code></td>
271 <td>The minute part of the current time </td></tr>
272 <tr><td><code>TIME_SEC</code></td>
273 <td>The second part of the current time </td></tr>
274 <tr><td><code>TIME_WDAY</code></td>
275 <td>The day of the week (starting with <code>0</code>
276 for Sunday)</td></tr>
277 <tr><td><code>TIME</code></td>
278 <td>The date and time in the format <code>20101231235959</code></td></tr>
279 <tr><td><code>SERVER_SOFTWARE</code></td>
280 <td>The server version string</td></tr>
281 <tr><td><code>API_VERSION</code></td>
282 <td>The date of the API version (module magic number)</td></tr>
285 <p>Some modules register additional variables, see e.g. <module>mod_ssl</module>.</p>
290 <title>Binary operators</title>
292 <p>With the exception of some built-in comparison operators, binary
293 operators have the form "<code>-[a-zA-Z][a-zA-Z0-9_]+</code>", i.e. a
294 minus and at least two characters. The name is not case sensitive.
295 Modules may register additional binary operators.</p>
298 <title>Comparison operators</title>
300 <table border="1" style="zebra">
301 <columnspec><column width=".2"/><column width=".2"/><column width=".6"/></columnspec>
303 <tr><th>Name</th><th>Alternative</th> <th>Description</th></tr>
304 <tr><td><code>==</code></td>
305 <td><code>=</code></td>
306 <td>String equality</td></tr>
307 <tr><td><code>!=</code></td>
309 <td>String inequality</td></tr>
310 <tr><td><code><</code></td>
312 <td>String less than</td></tr>
313 <tr><td><code><=</code></td>
315 <td>String less than or equal</td></tr>
316 <tr><td><code>></code></td>
318 <td>String greater than</td></tr>
319 <tr><td><code>>=</code></td>
321 <td>String greater than or equal</td></tr>
322 <tr><td><code>-eq</code></td>
323 <td><code>eq</code></td>
324 <td>Integer equality</td></tr>
325 <tr><td><code>-ne</code></td>
326 <td><code>ne</code></td>
327 <td>Integer inequality</td></tr>
328 <tr><td><code>-lt</code></td>
329 <td><code>lt</code></td>
330 <td>Integer less than</td></tr>
331 <tr><td><code>-le</code></td>
332 <td><code>le</code></td>
333 <td>Integer less than or equal</td></tr>
334 <tr><td><code>-gt</code></td>
335 <td><code>gt</code></td>
336 <td>Integer greater than</td></tr>
337 <tr><td><code>-ge</code></td>
338 <td><code>ge</code></td>
339 <td>Integer greater than or equal</td></tr>
343 <section id="binaryother">
344 <title>Other binary operators</title>
346 <table border="1" style="zebra">
347 <columnspec><column width=".2"/><column width=".8"/></columnspec>
349 <tr><th>Name</th><th>Description</th></tr>
350 <tr><td><code>-ipmatch</code></td>
351 <td>IP address matches address/netmask</td></tr>
352 <tr><td><code>-strmatch</code></td>
353 <td>left string matches pattern given by right string (containing
354 wildcards *, ?, [])</td></tr>
355 <tr><td><code>-strcmatch</code></td>
356 <td>same as <code>-strmatch</code>, but case insensitive</td></tr>
357 <tr><td><code>-fnmatch</code></td>
358 <td>same as <code>-strmatch</code>, but slashes are not matched by
366 <title>Unary operators</title>
368 <p>Unary operators take one argument and have the form
369 "<code>-[a-zA-Z]</code>", i.e. a minus and one character.
370 The name <em>is</em> case sensitive.
371 Modules may register additional unary operators.</p>
373 <table border="1" style="zebra">
374 <columnspec><column width=".2"/><column width=".2"/><column width=".6"/></columnspec>
376 <tr><th>Name</th><th>Description</th><th>Restricted</th></tr>
377 <tr><td><code>-d</code></td>
378 <td>The argument is treated as a filename.
379 True if the file exists and is a directory</td><td>yes</td></tr>
380 <tr><td><code>-e</code></td>
381 <td>The argument is treated as a filename.
382 True if the file (or dir or special) exists</td><td>yes</td></tr>
383 <tr><td><code>-f</code></td>
384 <td>The argument is treated as a filename.
385 True if the file exists and is regular file</td><td>yes</td></tr>
386 <tr><td><code>-s</code></td>
387 <td>The argument is treated as a filename.
388 True if the file exists and is not empty</td><td>yes</td></tr>
389 <tr><td><code>-L</code></td>
390 <td>The argument is treated as a filename.
391 True if the file exists and is symlink</td><td>yes</td></tr>
392 <tr><td><code>-h</code></td>
393 <td>The argument is treated as a filename.
394 True if the file exists and is symlink
395 (same as <code>-L</code>)</td><td>yes</td></tr>
396 <tr><td><code>-F</code></td>
397 <td>True if string is a valid file, accessible via all the server's
398 currently-configured access controls for that path. This uses an
399 internal subrequest to do the check, so use it with care - it can
400 impact your server's performance!</td><td></td></tr>
401 <tr><td><code>-U</code></td>
402 <td>True if string is a valid URL, accessible via all the server's
403 currently-configured access controls for that path. This uses an
404 internal subrequest to do the check, so use it with care - it can
405 impact your server's performance!</td><td></td></tr>
406 <tr><td><code>-A</code></td>
407 <td>Alias for <code>-U</code></td><td></td></tr>
408 <tr><td><code>-n</code></td>
409 <td>True if string is not empty</td><td></td></tr>
410 <tr><td><code>-z</code></td>
411 <td>True if string is empty</td><td></td></tr>
412 <tr><td><code>-T</code></td>
413 <td>False if string is empty, "<code>0</code>", "<code>off</code>",
414 "<code>false</code>", or "<code>no</code>" (case insensitive).
415 True otherwise.</td><td></td></tr>
416 <tr><td><code>-R</code></td>
417 <td>Same as "<code>%{REMOTE_ADDR} -ipmatch ...</code>", but more efficient
421 <p>The operators marked as "restricted" are not available in some modules
422 like <module>mod_include</module>.</p>
425 <section id="functions">
426 <title>Functions</title>
428 <p>Normal string-valued functions take one string as argument and return
429 a string. Functions names are not case sensitive.
430 Modules may register additional functions.</p>
432 <table border="1" style="zebra">
433 <columnspec><column width=".2"/><column width=".8"/></columnspec>
435 <tr><th>Name</th><th>Description</th><th>Restricted</th></tr>
436 <tr><td><code>req</code>, <code>http</code></td>
437 <td>Get HTTP request header; header names may be added to the Vary
438 header, see below</td><td></td></tr>
439 <tr><td><code>req_novary</code></td>
440 <td>Same as <code>req</code>, but header names will not be added to the
441 Vary header</td><td></td></tr>
442 <tr><td><code>resp</code></td>
443 <td>Get HTTP response header</td><td></td></tr>
444 <tr><td><code>reqenv</code></td>
445 <td>Lookup request environment variable</td><td></td></tr>
446 <tr><td><code>osenv</code></td>
447 <td>Lookup operating system environment variable</td><td></td></tr>
448 <tr><td><code>note</code></td>
449 <td>Lookup request note</td><td></td></tr>
450 <tr><td><code>env</code></td>
451 <td>Return first match of <code>note</code>, <code>reqenv</code>,
452 <code>osenv</code></td><td></td></tr>
453 <tr><td><code>tolower</code></td>
454 <td>Convert string to lower case</td><td></td></tr>
455 <tr><td><code>toupper</code></td>
456 <td>Convert string to uppser case</td><td></td></tr>
457 <tr><td><code>escape</code></td>
458 <td>Escape special characters in %hex encoding</td><td></td></tr>
459 <tr><td><code>unescape</code></td>
460 <td>Unescape %hex encoded string, leaving encoded slashes alone;
461 return empty string if %00 is found</td><td></td></tr>
462 <tr><td><code>file</code></td>
463 <td>Read contents from a file</td><td>yes</td></tr>
464 <tr><td><code>filesize</code></td>
465 <td>Return size of a file (or 0 if file does not exist or is not
466 regular file)</td><td>yes</td></tr>
470 <p>The functions marked as "restricted" are not available in some modules
471 like <module>mod_include</module>.</p>
473 <p>When the functions <code>req</code> or <code>http</code> are used,
474 the header name will automatically be added to the Vary header of the
475 HTTP response, except where otherwise noted for the directive accepting
476 the expression. The <code>req_novary</code> function can be used to
477 prevent names from being added to the Vary header.</p>
479 <p>In addition to string-valued functions, there are also list-valued functions which
480 take one string as argument and return a wordlist, i.e. a list of strings. The wordlist
481 can be used with the special <code>-in</code> operator.
482 Functions names are not case sensitive.
483 Modules may register additional functions.</p>
485 <p>There are no built-in list-valued functions. <module>mod_ssl</module>
486 provides <code>PeerExtList</code>. See the description of
487 <directive module="mod_ssl">SSLRequire</directive> for details
488 (but <code>PeerExtList</code> is also usable outside
489 of <directive module="mod_ssl">SSLRequire</directive>).</p>
493 <section id="examples">
495 <title>Example expressions</title>
496 <p>The following examples show how expressions might be used to evaluate requests:</p>
497 <!-- This section should probably be extended with more, useful examples -->
498 <highlight language="config">
499 # Compare the host name to example.com and redirect to www.example.com if it matches
500 <If "%{HTTP_HOST} == 'example.com'">
501 Redirect permanent / http://www.example.com
504 # Force text/plain if requesting a file with the query string contains 'forcetext'
505 <If "%{QUERY_STRING} =~ /forcetext/">
509 # Only allow access to this content during business hours
510 <Directory "/foo/bar/business">
511 Require expr %{TIME_HOUR} -gt 9 && %{TIME_HOUR} -lt 17
519 <table border="1" style="zebra">
520 <columnspec><column width=".2"/><column width=".2"/><column width=".6"/></columnspec>
522 <tr><th>Name</th><th>Alternative</th> <th>Description</th></tr>
523 <tr><td><code>-in</code></td>
524 <td><code>in</code></td>
525 <td>string contained in string list</td></tr>
526 <tr><td><code>/regexp/</code></td>
527 <td><code>m#regexp#</code></td>
528 <td>Regular expression (the second form allows different delimiters than /)</td></tr>
529 <tr><td><code>/regexp/i</code></td>
530 <td><code>m#regexp#i</code></td>
531 <td>Case insensitive regular expression</td></tr>
532 <tr><td><code>$0 ... $9</code></td>
534 <td>Regular expression backreferences</td></tr>
537 <section id="rebackref">
538 <title>Regular expression backreferences</title>
539 <p>The strings <code>$0</code> ... <code>$9</code> allow to reference
540 the capture groups from a previously executed, successfully
541 matching regular expressions. They can normally only be used in the
542 same expression as the matching regex, but some modules allow special
548 <section id="sslrequire">
549 <title>Comparison with SSLRequire</title>
550 <p>The <em>ap_expr</em> syntax is mostly a superset of the syntax of the
551 deprecated <directive module="mod_ssl">SSLRequire</directive> directive.
552 The differences are described in <directive
553 module="mod_ssl">SSLRequire</directive>'s documentation.</p>