2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.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 <modulesynopsis metafile="mod_setenvif.xml.meta">
25 <name>mod_setenvif</name>
26 <description>Allows the setting of environment variables based
27 on characteristics of the request</description>
29 <sourcefile>mod_setenvif.c</sourcefile>
30 <identifier>setenvif_module</identifier>
35 <p>The <module>mod_setenvif</module> module allows you to set
36 internal environment variables according to whether different aspects of
37 the request match regular expressions you specify. These
38 environment variables can be used by other parts of the server
39 to make decisions about actions to be taken, as well as becoming
40 available to CGI scripts and SSI pages.</p>
42 <p>The directives are considered in the order they appear in
43 the configuration files. So more complex sequences can be used,
44 such as this example, which sets <code>netscape</code> if the
45 browser is mozilla but not MSIE.</p>
47 <highlight language="config">
48 BrowserMatch ^Mozilla netscape
49 BrowserMatch MSIE !netscape
53 <seealso><a href="../env.html">Environment Variables in Apache HTTP Server</a></seealso>
56 <name>BrowserMatch</name>
57 <description>Sets environment variables conditional on HTTP User-Agent
59 <syntax>BrowserMatch <em>regex [!]env-variable</em>[=<em>value</em>]
60 [[!]<em>env-variable</em>[=<em>value</em>]] ...</syntax>
61 <contextlist><context>server config</context>
62 <context>virtual host</context><context>directory</context>
63 <context>.htaccess</context></contextlist>
64 <override>FileInfo</override>
67 <p>The <directive>BrowserMatch</directive> is a special cases of the
68 <directive module="mod_setenvif">SetEnvIf</directive> directive that
69 sets environment variables conditional on the
70 <code>User-Agent</code> HTTP request header. The following two
71 lines have the same effect:</p>
72 <highlight language="config">
73 BrowserMatchNoCase Robot is_a_robot
74 SetEnvIfNoCase User-Agent Robot is_a_robot
77 <p>Some additional examples:</p>
78 <highlight language="config">
79 BrowserMatch ^Mozilla forms jpeg=yes browser=netscape
80 BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript
81 BrowserMatch MSIE !javascript
87 <name>BrowserMatchNoCase</name>
88 <description>Sets environment variables conditional on User-Agent without
89 respect to case</description>
90 <syntax>BrowserMatchNoCase <em>regex [!]env-variable</em>[=<em>value</em>]
91 [[!]<em>env-variable</em>[=<em>value</em>]] ...</syntax>
92 <contextlist><context>server config</context>
93 <context>virtual host</context><context>directory</context>
94 <context>.htaccess</context></contextlist>
95 <override>FileInfo</override>
99 <p>The <directive>BrowserMatchNoCase</directive> directive is
100 semantically identical to the <directive
101 module="mod_setenvif">BrowserMatch</directive> directive.
102 However, it provides for case-insensitive matching. For
104 <highlight language="config">
105 BrowserMatchNoCase mac platform=macintosh
106 BrowserMatchNoCase win platform=windows
109 <p>The <directive>BrowserMatch</directive> and
110 <directive>BrowserMatchNoCase</directive> directives are special cases of
111 the <directive module="mod_setenvif">SetEnvIf</directive> and <directive
112 module="mod_setenvif">SetEnvIfNoCase</directive>
113 directives. The following two lines have the same effect:</p>
114 <highlight language="config">
115 BrowserMatchNoCase Robot is_a_robot
116 SetEnvIfNoCase User-Agent Robot is_a_robot
122 <name>SetEnvIf</name>
123 <description>Sets environment variables based on attributes of the request
125 <syntax>SetEnvIf <em>attribute
126 regex [!]env-variable</em>[=<em>value</em>]
127 [[!]<em>env-variable</em>[=<em>value</em>]] ...</syntax>
128 <contextlist><context>server config</context>
129 <context>virtual host</context><context>directory</context>
130 <context>.htaccess</context></contextlist>
131 <override>FileInfo</override>
134 <p>The <directive>SetEnvIf</directive> directive defines
135 environment variables based on attributes of the request. The
136 <em>attribute</em> specified in the first argument can be one of four
140 <li>An HTTP request header field (see <a
141 href="http://www.rfc-editor.org/rfc/rfc2616.txt">RFC2616</a>
142 for more information about these); for example: <code>Host</code>,
143 <code>User-Agent</code>, <code>Referer</code>, and
144 <code>Accept-Language</code>. A regular expression may be
145 used to specify a set of request headers.</li>
147 <li>One of the following aspects of the request:
149 <li><code>Remote_Host</code> - the hostname (if available) of
150 the client making the request</li>
152 <li><code>Remote_Addr</code> - the IP address of the client
153 making the request</li>
155 <li><code>Server_Addr</code> - the IP address of the server
156 on which the request was received (only with versions later
159 <li><code>Request_Method</code> - the name of the method
160 being used (<code>GET</code>, <code>POST</code>, <em>et
163 <li><code>Request_Protocol</code> - the name and version of
164 the protocol with which the request was made (<em>e.g.</em>,
165 "HTTP/0.9", "HTTP/1.1", <em>etc.</em>)</li>
167 <li><code>Request_URI</code> - the resource requested on the HTTP
168 request line -- generally the portion of the URL
169 following the scheme and host portion without the query string. See
170 the <directive module="mod_rewrite">RewriteCond</directive>
171 directive of <module>mod_rewrite</module> for extra information on
172 how to match your query string.</li>
176 <li>The name of an environment variable in the list of those
177 associated with the request. This allows
178 <directive>SetEnvIf</directive> directives to test against the result
179 of prior matches. Only those environment variables defined by earlier
180 <code>SetEnvIf[NoCase]</code> directives are available for testing in
181 this manner. 'Earlier' means that they were defined at a broader scope
182 (such as server-wide) or previously in the current directive's scope.
183 Environment variables will be considered only if there was no match
184 among request characteristics and a regular expression was not
185 used for the <em>attribute</em>.</li>
189 <p>The second argument (<em>regex</em>) is a <glossary
190 ref="regex">regular expression</glossary>. If the <em>regex</em>
191 matches against the <em>attribute</em>, then the remainder of the
192 arguments are evaluated.</p>
194 <p>The rest of the arguments give the names of variables to set, and
195 optionally values to which they should be set. These take the form
199 <li><code><em>varname</em></code>, or</li>
201 <li><code>!<em>varname</em></code>, or</li>
203 <li><code><em>varname</em>=<em>value</em></code></li>
206 <p>In the first form, the value will be set to "1". The second
207 will remove the given variable if already defined, and the
208 third will set the variable to the literal value given by
209 <code><em>value</em></code>. Since version 2.0.51, Apache httpd will
210 recognize occurrences of <code>$1</code>..<code>$9</code> within
211 <var>value</var> and replace them by parenthesized subexpressions
212 of <var>regex</var>.</p>
214 <highlight language="config">
215 SetEnvIf Request_URI "\.gif$" object_is_image=gif
216 SetEnvIf Request_URI "\.jpg$" object_is_image=jpg
217 SetEnvIf Request_URI "\.xbm$" object_is_image=xbm
219 SetEnvIf Referer www\.mydomain\.example\.com intra_site_referral
221 SetEnvIf object_is_image xbm XBIT_PROCESSING=1
223 SetEnvIf ^TS ^[a-z] HAVE_TS
226 <p>The first three will set the environment variable
227 <code>object_is_image</code> if the request was for an image
228 file, and the fourth sets <code>intra_site_referral</code> if
229 the referring page was somewhere on the
230 <code>www.mydomain.example.com</code> Web site.</p>
232 <p>The last example will set environment variable
233 <code>HAVE_TS</code> if the request contains any headers that
234 begin with "TS" whose values begins with any character in the
238 <seealso><a href="../env.html">Environment Variables in Apache HTTP Server</a>,
239 for additional examples.
246 <name>SetEnvIfExpr</name>
247 <description>Sets environment variables based on an ap_expr expression</description>
248 <syntax>SetEnvIfExpr <em>expr
249 [!]env-variable</em>[=<em>value</em>]
250 [[!]<em>env-variable</em>[=<em>value</em>]] ...</syntax>
251 <contextlist><context>server config</context>
252 <context>virtual host</context><context>directory</context>
253 <context>.htaccess</context></contextlist>
254 <override>FileInfo</override>
257 <p>The <directive>SetEnvIfExpr</directive> directive defines
258 environment variables based on an <directive type="section">If</directive>
259 <code>ap_expr</code>. These expressions will be evaluated at runtime,
260 and applied <em>env-variable</em> in the same fashion as <directive
261 >SetEnvIf</directive>.</p>
263 <highlight language="config">
264 SetEnvIfExpr "tolower(req('X-Sendfile')) == 'd:\images\very_big.iso')" iso_delivered
267 <p>This would set the environment variable <code>iso_delivered</code>
268 every time our application attempts to send it via <code>X-Sendfile</code></p>
270 <p>A more useful example would be to set the variable rfc1918 if the
271 remote IP address is a private address according to RFC 1918:</p>
273 <highlight language="config">
274 SetEnvIfExpr "-R '10.0.0.0/8' || -R '172.16.0.0/12' || -R '192.168.0.0/16'" rfc1918
278 <seealso><a href="../expr.html">Expressions in Apache HTTP Server</a>,
279 for a complete reference and more examples.</seealso>
280 <seealso><directive type="section">If</directive> can be used to achive similar
282 <seealso><module>mod_filter</module></seealso>
286 <name>SetEnvIfNoCase</name>
287 <description>Sets environment variables based on attributes of the request
288 without respect to case</description>
289 <syntax>SetEnvIfNoCase <em>attribute regex
290 [!]env-variable</em>[=<em>value</em>]
291 [[!]<em>env-variable</em>[=<em>value</em>]] ...</syntax>
292 <contextlist><context>server config</context>
293 <context>virtual host</context><context>directory</context>
294 <context>.htaccess</context></contextlist>
295 <override>FileInfo</override>
299 <p>The <directive>SetEnvIfNoCase</directive> is semantically identical to
300 the <directive module="mod_setenvif">SetEnvIf</directive> directive,
301 and differs only in that the regular expression matching is
302 performed in a case-insensitive manner. For example:</p>
303 <highlight language="config">
304 SetEnvIfNoCase Host Example\.Org site=example
307 <p>This will cause the <code>site</code> environment variable
308 to be set to "<code>example</code>" if the HTTP request header
309 field <code>Host:</code> was included and contained
310 <code>Example.Org</code>, <code>example.org</code>, or any other