2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
6 Copyright 2002-2004 The Apache Software Foundation
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
21 <modulesynopsis metafile="mod_setenvif.xml.meta">
23 <name>mod_setenvif</name>
24 <description>Allows the setting of environment variables based
25 on characteristics of the request</description>
27 <sourcefile>mod_setenvif.c</sourcefile>
28 <identifier>setenvif_module</identifier>
33 <p>The <module>mod_setenvif</module> module allows you to set
34 environment variables according to whether different aspects of
35 the request match regular expressions you specify. These
36 environment variables can be used by other parts of the server
37 to make decisions about actions to be taken.</p>
39 <p>The directives are considered in the order they appear in
40 the configuration files. So more complex sequences can be used,
41 such as this example, which sets <code>netscape</code> if the
42 browser is mozilla but not MSIE.</p>
45 BrowserMatch ^Mozilla netscape<br />
46 BrowserMatch MSIE !netscape<br />
50 <seealso><a href="../env.html">Environment Variables in Apache</a></seealso>
53 <name>BrowserMatch</name>
54 <description>Sets environment variables conditional on HTTP User-Agent
56 <syntax>BrowserMatch <em>regex [!]env-variable</em>[=<em>value</em>]
57 [[!]<em>env-variable</em>[=<em>value</em>]] ...</syntax>
58 <contextlist><context>server config</context>
59 <context>virtual host</context><context>directory</context>
60 <context>.htaccess</context></contextlist>
61 <override>FileInfo</override>
64 <p>The <directive>BrowserMatch</directive> is a special cases of the
65 <directive module="mod_setenvif">SetEnvIf</directive> directive that
66 sets environment variables conditional on the
67 <code>User-Agent</code> HTTP request header. The following two
68 lines have the same effect:</p>
70 BrowserMatchNoCase Robot is_a_robot<br />
71 SetEnvIfNoCase User-Agent Robot is_a_robot<br />
74 <p>Some additional examples:</p>
76 BrowserMatch ^Mozilla forms jpeg=yes browser=netscape<br />
77 BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript<br />
78 BrowserMatch MSIE !javascript<br />
84 <name>BrowserMatchNoCase</name>
85 <description>Sets environment variables conditional on User-Agent without
86 respect to case</description>
87 <syntax>BrowserMatchNoCase <em>regex [!]env-variable</em>[=<em>value</em>]
88 [[!]<em>env-variable</em>[=<em>value</em>]] ...</syntax>
89 <contextlist><context>server config</context>
90 <context>virtual host</context><context>directory</context>
91 <context>.htaccess</context></contextlist>
92 <override>FileInfo</override>
93 <compatibility>Apache 1.2 and
94 above (in Apache 1.2 this directive was found in the
95 now-obsolete mod_browser module)</compatibility>
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
105 BrowserMatchNoCase mac platform=macintosh<br />
106 BrowserMatchNoCase win platform=windows<br />
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>
115 BrowserMatchNoCase Robot is_a_robot<br />
116 SetEnvIfNoCase User-Agent Robot is_a_robot<br />
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 three
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</li>
173 <li>The name of an environment variable in the list of those
174 associated with the request. This allows
175 <directive>SetEnvIf</directive> directives to test against the result
176 of prior matches. Only those environment variables defined by earlier
177 <code>SetEnvIf[NoCase]</code> directives are available for testing in
178 this manner. 'Earlier' means that they were defined at a broader scope
179 (such as server-wide) or previously in the current directive's scope.
180 Environment variables will be considered only if there was no match
181 among request characteristics and a regular expression was not
182 used for the <em>attribute</em>.</li>
185 <p>The second argument (<em>regex</em>) is a <a
186 href="http://www.pcre.org/">Perl compatible regular expression</a>.
187 This is similar to a POSIX.2 egrep-style regular expression.
188 If the <em>regex</em> matches against the <em>attribute</em>,
189 then the remainder of the arguments are evaluated.</p>
191 <p>The rest of the arguments give the names of variables to set, and
192 optionally values to which they should be set. These take the form
196 <li><code><em>varname</em></code>, or</li>
198 <li><code>!<em>varname</em></code>, or</li>
200 <li><code><em>varname</em>=<em>value</em></code></li>
203 <p>In the first form, the value will be set to "1". The second
204 will remove the given variable if already defined, and the
205 third will set the variable to the literal value given by
206 <code><em>value</em></code>. Since version 2.1 Apache will
207 recognize occurrences of <code>$1</code>..<code>$9</code> within
208 <var>value</var> and replace them by parenthesized subexpressions
209 of <var>regex</var>.</p>
212 <title>Example:</title>
213 SetEnvIf Request_URI "\.gif$" object_is_image=gif<br />
214 SetEnvIf Request_URI "\.jpg$" object_is_image=jpg<br />
215 SetEnvIf Request_URI "\.xbm$" object_is_image=xbm<br />
217 SetEnvIf Referer www\.mydomain\.com intra_site_referral<br />
219 SetEnvIf object_is_image xbm XBIT_PROCESSING=1<br />
221 SetEnvIf ^TS* ^[a-z].* HAVE_TS<br />
224 <p>The first three will set the environment variable
225 <code>object_is_image</code> if the request was for an image
226 file, and the fourth sets <code>intra_site_referral</code> if
227 the referring page was somewhere on the
228 <code>www.mydomain.com</code> Web site.</p>
230 <p>The last example will set environment variable
231 <code>HAVE_TS</code> if the request contains any headers that
232 begin with "TS" whose values begins with any character in the
236 <seealso><a href="../env.html">Environment Variables in Apache</a>,
237 for additional examples.
242 <name>SetEnvIfNoCase</name>
243 <description>Sets environment variables based on attributes of the request
244 without respect to case</description>
245 <syntax>SetEnvIfNoCase <em>attribute regex
246 [!]env-variable</em>[=<em>value</em>]
247 [[!]<em>env-variable</em>[=<em>value</em>]] ...</syntax>
248 <contextlist><context>server config</context>
249 <context>virtual host</context><context>directory</context>
250 <context>.htaccess</context></contextlist>
251 <override>FileInfo</override>
252 <compatibility>Apache 1.3 and above</compatibility>
256 <p>The <directive>SetEnvIfNoCase</directive> is semantically identical to
257 the <directive module="mod_setenvif">SetEnvIf</directive> directive,
258 and differs only in that the regular expression matching is
259 performed in a case-insensitive manner. For example:</p>
261 SetEnvIfNoCase Host Apache\.Org site=apache
264 <p>This will cause the <code>site</code> environment variable
265 to be set to "<code>apache</code>" if the HTTP request header
266 field <code>Host:</code> was included and contained
267 <code>Apache.Org</code>, <code>apache.org</code>, or any other