1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
4 <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7 This file is generated from xml source: DO NOT EDIT
8 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10 <title>mod_setenvif - Apache HTTP Server Version 2.5</title>
11 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
12 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
13 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
14 <script src="../style/scripts/prettify.min.js" type="text/javascript">
17 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
19 <div id="page-header">
20 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
21 <p class="apache">Apache HTTP Server Version 2.5</p>
22 <img alt="" src="../images/feather.png" /></div>
23 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
25 <a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a> > <a href="./">Modules</a></div>
26 <div id="page-content">
27 <div id="preamble"><h1>Apache Module mod_setenvif</h1>
29 <p><span>Available Languages: </span><a href="../en/mod/mod_setenvif.html" title="English"> en </a> |
30 <a href="../fr/mod/mod_setenvif.html" hreflang="fr" rel="alternate" title="Français"> fr </a> |
31 <a href="../ja/mod/mod_setenvif.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
32 <a href="../ko/mod/mod_setenvif.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
33 <a href="../tr/mod/mod_setenvif.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
35 <table class="module"><tr><th><a href="module-dict.html#Description">Description:</a></th><td>Allows the setting of environment variables based
36 on characteristics of the request</td></tr>
37 <tr><th><a href="module-dict.html#Status">Status:</a></th><td>Base</td></tr>
38 <tr><th><a href="module-dict.html#ModuleIdentifier">Module Identifier:</a></th><td>setenvif_module</td></tr>
39 <tr><th><a href="module-dict.html#SourceFile">Source File:</a></th><td>mod_setenvif.c</td></tr></table>
43 <p>The <code class="module"><a href="../mod/mod_setenvif.html">mod_setenvif</a></code> module allows you to set
44 internal environment variables according to whether different aspects of
45 the request match regular expressions you specify. These
46 environment variables can be used by other parts of the server
47 to make decisions about actions to be taken, as well as becoming
48 available to CGI scripts and SSI pages.</p>
50 <p>The directives are considered in the order they appear in
51 the configuration files. So more complex sequences can be used,
52 such as this example, which sets <code>netscape</code> if the
53 browser is mozilla but not MSIE.</p>
55 <pre class="prettyprint lang-config">BrowserMatch ^Mozilla netscape
56 BrowserMatch MSIE !netscape</pre>
59 <p>When the server looks up a path via an internal
60 <a class="glossarylink" href="../glossary.html#subrequest" title="see glossary">subrequest</a> such as looking
61 for a <code class="directive"><a href="../mod/mod_dir.html#directoryindex">DirectoryIndex</a></code>
62 or generating a directory listing with <code class="module"><a href="../mod/mod_autoindex.html">mod_autoindex</a></code>,
63 per-request environment variables are <em>not</em> inherited in the
64 subrequest. Additionally,
65 <code class="directive"><a href="#setenvif">SetEnvIf</a></code> directives
66 are not separately evaluated in the subrequest due to the API phases
67 <code class="module"><a href="../mod/mod_setenvif.html">mod_setenvif</a></code> takes action in.</p>
70 <div id="quickview"><h3 class="directives">Directives</h3>
72 <li><img alt="" src="../images/down.gif" /> <a href="#browsermatch">BrowserMatch</a></li>
73 <li><img alt="" src="../images/down.gif" /> <a href="#browsermatchnocase">BrowserMatchNoCase</a></li>
74 <li><img alt="" src="../images/down.gif" /> <a href="#setenvif">SetEnvIf</a></li>
75 <li><img alt="" src="../images/down.gif" /> <a href="#setenvifexpr">SetEnvIfExpr</a></li>
76 <li><img alt="" src="../images/down.gif" /> <a href="#setenvifnocase">SetEnvIfNoCase</a></li>
78 <h3>Bugfix checklist:</h3><ul class="seealso"><li><a href="https://www.apache.org/dist/httpd/CHANGES_2.4">HTTPD changelog</a></li><li><a href="https://bz.apache.org/bugzilla/buglist.cgi?bug_status=__open__&list_id=144532&product=Apache%20httpd-2&query_format=specific&order=changeddate%20DESC%2Cpriority%2Cbug_severity&component=mod_setenvif">Known issues</a></li><li><a href="https://bz.apache.org/bugzilla/enter_bug.cgi?product=Apache%20httpd-2">Report a bug</a></li></ul><h3>See also</h3>
80 <li><a href="../env.html">Environment Variables in Apache HTTP Server</a></li>
81 </ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
83 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
84 <div class="directive-section"><h2><a name="BrowserMatch" id="BrowserMatch">BrowserMatch</a> <a name="browsermatch" id="browsermatch">Directive</a></h2>
85 <table class="directive">
86 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables conditional on HTTP User-Agent
88 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>BrowserMatch <em>regex [!]env-variable</em>[=<em>value</em>]
89 [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
90 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
91 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
92 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
93 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
95 <p>The <code class="directive">BrowserMatch</code> is a special cases of the
96 <code class="directive"><a href="#setenvif">SetEnvIf</a></code> directive that
97 sets environment variables conditional on the
98 <code>User-Agent</code> HTTP request header. The following two
99 lines have the same effect:</p>
100 <pre class="prettyprint lang-config">BrowserMatch Robot is_a_robot
101 SetEnvIf User-Agent Robot is_a_robot</pre>
104 <p>Some additional examples:</p>
105 <pre class="prettyprint lang-config">BrowserMatch ^Mozilla forms jpeg=yes browser=netscape
106 BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript
107 BrowserMatch MSIE !javascript</pre>
111 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
112 <div class="directive-section"><h2><a name="BrowserMatchNoCase" id="BrowserMatchNoCase">BrowserMatchNoCase</a> <a name="browsermatchnocase" id="browsermatchnocase">Directive</a></h2>
113 <table class="directive">
114 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables conditional on User-Agent without
115 respect to case</td></tr>
116 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>BrowserMatchNoCase <em>regex [!]env-variable</em>[=<em>value</em>]
117 [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
118 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
119 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
120 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
121 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
124 <p>The <code class="directive">BrowserMatchNoCase</code> directive is
125 semantically identical to the <code class="directive"><a href="#browsermatch">BrowserMatch</a></code> directive.
126 However, it provides for case-insensitive matching. For
128 <pre class="prettyprint lang-config">BrowserMatchNoCase mac platform=macintosh
129 BrowserMatchNoCase win platform=windows</pre>
132 <p>The <code class="directive">BrowserMatch</code> and
133 <code class="directive">BrowserMatchNoCase</code> directives are special cases of
134 the <code class="directive"><a href="#setenvif">SetEnvIf</a></code> and <code class="directive"><a href="#setenvifnocase">SetEnvIfNoCase</a></code>
135 directives. The following two lines have the same effect:</p>
136 <pre class="prettyprint lang-config">BrowserMatchNoCase Robot is_a_robot
137 SetEnvIfNoCase User-Agent Robot is_a_robot</pre>
141 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
142 <div class="directive-section"><h2><a name="SetEnvIf" id="SetEnvIf">SetEnvIf</a> <a name="setenvif" id="setenvif">Directive</a></h2>
143 <table class="directive">
144 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables based on attributes of the request
146 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>SetEnvIf <em>attribute
147 regex [!]env-variable</em>[=<em>value</em>]
148 [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
149 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
150 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
151 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
152 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
154 <p>The <code class="directive">SetEnvIf</code> directive defines
155 environment variables based on attributes of the request. The
156 <em>attribute</em> specified in the first argument can be one of four
160 <li>An HTTP request header field (see <a href="http://www.rfc-editor.org/rfc/rfc2616.txt">RFC2616</a>
161 for more information about these); for example: <code>Host</code>,
162 <code>User-Agent</code>, <code>Referer</code>, and
163 <code>Accept-Language</code>. A regular expression may be
164 used to specify a set of request headers.</li>
166 <li>One of the following aspects of the request:
168 <li><code>Remote_Host</code> - the hostname (if available) of
169 the client making the request</li>
171 <li><code>Remote_Addr</code> - the IP address of the client
172 making the request</li>
174 <li><code>Server_Addr</code> - the IP address of the server
175 on which the request was received (only with versions later
178 <li><code>Request_Method</code> - the name of the method
179 being used (<code>GET</code>, <code>POST</code>, <em>et
182 <li><code>Request_Protocol</code> - the name and version of
183 the protocol with which the request was made (<em>e.g.</em>,
184 "HTTP/0.9", "HTTP/1.1", <em>etc.</em>)</li>
186 <li><code>Request_URI</code> - the resource requested on the HTTP
187 request line -- generally the portion of the URL
188 following the scheme and host portion without the query string. See
189 the <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>
190 directive of <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> for extra information on
191 how to match your query string.</li>
195 <li>The name of an environment variable in the list of those
196 associated with the request. This allows
197 <code class="directive">SetEnvIf</code> directives to test against the result
198 of prior matches. Only those environment variables defined by earlier
199 <code>SetEnvIf[NoCase]</code> directives are available for testing in
200 this manner. 'Earlier' means that they were defined at a broader scope
201 (such as server-wide) or previously in the current directive's scope.
202 Environment variables will be considered only if there was no match
203 among request characteristics and a regular expression was not
204 used for the <em>attribute</em>.</li>
208 <p>The second argument (<em>regex</em>) is a <a class="glossarylink" href="../glossary.html#regex" title="see glossary">regular expression</a>. If the <em>regex</em>
209 matches against the <em>attribute</em>, then the remainder of the
210 arguments are evaluated.</p>
212 <p>The rest of the arguments give the names of variables to set, and
213 optionally values to which they should be set. These take the form
217 <li><code><em>varname</em></code>, or</li>
219 <li><code>!<em>varname</em></code>, or</li>
221 <li><code><em>varname</em>=<em>value</em></code></li>
224 <p>In the first form, the value will be set to "1". The second
225 will remove the given variable if already defined, and the
226 third will set the variable to the literal value given by
227 <code><em>value</em></code>. Since version 2.0.51, Apache httpd will
228 recognize occurrences of <code>$1</code>..<code>$9</code> within
229 <var>value</var> and replace them by parenthesized subexpressions
230 of <var>regex</var>. <code>$0</code> provides access to the whole
231 string matched by that pattern.</p>
233 <pre class="prettyprint lang-config">SetEnvIf Request_URI "\.gif$" object_is_image=gif
234 SetEnvIf Request_URI "\.jpg$" object_is_image=jpg
235 SetEnvIf Request_URI "\.xbm$" object_is_image=xbm
237 SetEnvIf Referer www\.mydomain\.example\.com intra_site_referral
239 SetEnvIf object_is_image xbm XBIT_PROCESSING=1
241 SetEnvIf Request_URI "\.(.*)$" EXTENSION=$1
243 SetEnvIf ^TS ^[a-z] HAVE_TS</pre>
246 <p>The first three will set the environment variable
247 <code>object_is_image</code> if the request was for an image
248 file, and the fourth sets <code>intra_site_referral</code> if
249 the referring page was somewhere on the
250 <code>www.mydomain.example.com</code> Web site.</p>
252 <p>The last example will set environment variable
253 <code>HAVE_TS</code> if the request contains any headers that
254 begin with "TS" whose values begins with any character in the
259 <li><a href="../env.html">Environment Variables in Apache HTTP Server</a>,
260 for additional examples.
264 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
265 <div class="directive-section"><h2><a name="SetEnvIfExpr" id="SetEnvIfExpr">SetEnvIfExpr</a> <a name="setenvifexpr" id="setenvifexpr">Directive</a></h2>
266 <table class="directive">
267 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables based on an ap_expr expression</td></tr>
268 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>SetEnvIfExpr <em>expr
269 [!]env-variable</em>[=<em>value</em>]
270 [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
271 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
272 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
273 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
274 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
276 <p>The <code class="directive">SetEnvIfExpr</code> directive defines
277 environment variables based on an <code class="directive"><a href="../mod/core.html#if"><If></a></code>
278 <code>ap_expr</code>. These expressions will be evaluated at runtime,
279 and applied <em>env-variable</em> in the same fashion as <code class="directive">SetEnvIf</code>.</p>
281 <pre class="prettyprint lang-config">SetEnvIfExpr "tolower(req('X-Sendfile')) == 'd:\images\very_big.iso')" iso_delivered</pre>
284 <p>This would set the environment variable <code>iso_delivered</code>
285 every time our application attempts to send it via <code>X-Sendfile</code></p>
287 <p>A more useful example would be to set the variable rfc1918 if the
288 remote IP address is a private address according to RFC 1918:</p>
290 <pre class="prettyprint lang-config">SetEnvIfExpr "-R '10.0.0.0/8' || -R '172.16.0.0/12' || -R '192.168.0.0/16'" rfc1918</pre>
295 <li><a href="../expr.html">Expressions in Apache HTTP Server</a>,
296 for a complete reference and more examples.</li>
297 <li><code class="directive"><a href="../mod/core.html#if"><If></a></code> can be used to achieve similar
299 <li><code class="module"><a href="../mod/mod_filter.html">mod_filter</a></code></li>
302 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
303 <div class="directive-section"><h2><a name="SetEnvIfNoCase" id="SetEnvIfNoCase">SetEnvIfNoCase</a> <a name="setenvifnocase" id="setenvifnocase">Directive</a></h2>
304 <table class="directive">
305 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables based on attributes of the request
306 without respect to case</td></tr>
307 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>SetEnvIfNoCase <em>attribute regex
308 [!]env-variable</em>[=<em>value</em>]
309 [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
310 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
311 <tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
312 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
313 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
316 <p>The <code class="directive">SetEnvIfNoCase</code> is semantically identical to
317 the <code class="directive"><a href="#setenvif">SetEnvIf</a></code> directive,
318 and differs only in that the regular expression matching is
319 performed in a case-insensitive manner. For example:</p>
320 <pre class="prettyprint lang-config">SetEnvIfNoCase Host Example\.Org site=example</pre>
323 <p>This will cause the <code>site</code> environment variable
324 to be set to "<code>example</code>" if the HTTP request header
325 field <code>Host:</code> was included and contained
326 <code>Example.Org</code>, <code>example.org</code>, or any other
331 <div class="bottomlang">
332 <p><span>Available Languages: </span><a href="../en/mod/mod_setenvif.html" title="English"> en </a> |
333 <a href="../fr/mod/mod_setenvif.html" hreflang="fr" rel="alternate" title="Français"> fr </a> |
334 <a href="../ja/mod/mod_setenvif.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
335 <a href="../ko/mod/mod_setenvif.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
336 <a href="../tr/mod/mod_setenvif.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
337 </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />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 <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
338 <script type="text/javascript"><!--//--><![CDATA[//><!--
339 var comments_shortname = 'httpd';
340 var comments_identifier = 'http://httpd.apache.org/docs/trunk/mod/mod_setenvif.html';
342 if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
343 d.write('<div id="comments_thread"><\/div>');
344 var s = d.createElement('script');
345 s.type = 'text/javascript';
347 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
348 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
351 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
353 })(window, document);
354 //--><!]]></script></div><div id="footer">
355 <p class="apache">Copyright 2016 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
356 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
357 if (typeof(prettyPrint) !== 'undefined') {