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        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5              This file is generated from xml source: DO NOT EDIT
6        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7      -->
8<title>mod_setenvif - Apache HTTP Server</title>
9<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11<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" />
12<script src="/style/scripts/prettify.min.js" type="text/javascript">
13</script>
14
15<link href="/images/favicon.ico" rel="shortcut icon" /></head>
16<body>
17<div id="page-header">
18<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.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>
19<p class="apache">Apache HTTP Server Version 2.4</p>
20<img alt="" src="/images/feather.gif" /></div>
21<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
22<div id="path">
23<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.4</a> &gt; <a href="./">Modules</a></div>
24<div id="page-content">
25<div id="preamble"><h1>Apache Module mod_setenvif</h1>
26<div class="toplang">
27<p><span>Available Languages: </span><a href="/en/mod/mod_setenvif.html" title="English">&nbsp;en&nbsp;</a> |
28<a href="/fr/mod/mod_setenvif.html" hreflang="fr" rel="alternate" title="Fran�ais">&nbsp;fr&nbsp;</a> |
29<a href="/ja/mod/mod_setenvif.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
30<a href="/ko/mod/mod_setenvif.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
31<a href="/tr/mod/mod_setenvif.html" hreflang="tr" rel="alternate" title="T�rk�e">&nbsp;tr&nbsp;</a></p>
32</div>
33<table class="module"><tr><th><a href="module-dict.html#Description">Description:</a></th><td>Allows the setting of environment variables based
34on characteristics of the request</td></tr>
35<tr><th><a href="module-dict.html#Status">Status:</a></th><td>Base</td></tr>
36<tr><th><a href="module-dict.html#ModuleIdentifier">Module�Identifier:</a></th><td>setenvif_module</td></tr>
37<tr><th><a href="module-dict.html#SourceFile">Source�File:</a></th><td>mod_setenvif.c</td></tr></table>
38<h3>Summary</h3>
39
40
41    <p>The <code class="module"><a href="/mod/mod_setenvif.html">mod_setenvif</a></code> module allows you to set
42    internal environment variables according to whether different aspects of
43    the request match regular expressions you specify. These
44    environment variables can be used by other parts of the server
45    to make decisions about actions to be taken, as well as becoming
46    available to CGI scripts and SSI pages.</p>
47
48    <p>The directives are considered in the order they appear in
49    the configuration files. So more complex sequences can be used,
50    such as this example, which sets <code>netscape</code> if the
51    browser is mozilla but not MSIE.</p>
52
53<pre class="prettyprint lang-config">BrowserMatch ^Mozilla netscape
54BrowserMatch MSIE !netscape</pre>
55
56
57   <p>When the server looks up a path via an internal 
58   <a class="glossarylink" href="/glossary.html#subrequest" title="see glossary">subrequest</a> such as looking 
59   for a <code class="directive"><a href="/mod/mod_dir.html#directoryindex">DirectoryIndex</a></code> 
60   or generating a directory listing with <code class="module"><a href="/mod/mod_autoindex.html">mod_autoindex</a></code>,
61   per-request environment variables are <em>not</em> inherited in the 
62   subrequest. Additionally, 
63   <code class="directive"><a href="#setenvif">SetEnvIf</a></code> directives
64   are not separately evaluated in the subrequest due to the API phases
65   <code class="module"><a href="/mod/mod_setenvif.html">mod_setenvif</a></code> takes action in.</p>
66
67</div>
68<div id="quickview"><h3 class="directives">Directives</h3>
69<ul id="toc">
70<li><img alt="" src="/images/down.gif" /> <a href="#browsermatch">BrowserMatch</a></li>
71<li><img alt="" src="/images/down.gif" /> <a href="#browsermatchnocase">BrowserMatchNoCase</a></li>
72<li><img alt="" src="/images/down.gif" /> <a href="#setenvif">SetEnvIf</a></li>
73<li><img alt="" src="/images/down.gif" /> <a href="#setenvifexpr">SetEnvIfExpr</a></li>
74<li><img alt="" src="/images/down.gif" /> <a href="#setenvifnocase">SetEnvIfNoCase</a></li>
75</ul>
76<h3>See also</h3>
77<ul class="seealso">
78<li><a href="/env.html">Environment Variables in Apache HTTP Server</a></li>
79</ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
80
81<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
82<div class="directive-section"><h2><a name="BrowserMatch" id="BrowserMatch">BrowserMatch</a> <a name="browsermatch" id="browsermatch">Directive</a></h2>
83<table class="directive">
84<tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables conditional on HTTP User-Agent
85</td></tr>
86<tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>BrowserMatch <em>regex [!]env-variable</em>[=<em>value</em>]
87[[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
88<tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
89<tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
90<tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
91<tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
92</table>
93  <p>The <code class="directive">BrowserMatch</code> is a special cases of the
94  <code class="directive"><a href="#setenvif">SetEnvIf</a></code> directive that
95  sets environment variables conditional on the
96  <code>User-Agent</code> HTTP request header.  The following two
97  lines have the same effect:</p>
98<pre class="prettyprint lang-config">BrowserMatchNoCase Robot is_a_robot
99SetEnvIfNoCase User-Agent Robot is_a_robot</pre>
100
101
102    <p>Some additional examples:</p>
103<pre class="prettyprint lang-config">BrowserMatch ^Mozilla forms jpeg=yes browser=netscape
104BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript
105BrowserMatch MSIE !javascript</pre>
106
107
108</div>
109<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
110<div class="directive-section"><h2><a name="BrowserMatchNoCase" id="BrowserMatchNoCase">BrowserMatchNoCase</a> <a name="browsermatchnocase" id="browsermatchnocase">Directive</a></h2>
111<table class="directive">
112<tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables conditional on User-Agent without
113respect to case</td></tr>
114<tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>BrowserMatchNoCase  <em>regex [!]env-variable</em>[=<em>value</em>]
115    [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
116<tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
117<tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
118<tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
119<tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
120</table>
121
122    <p>The <code class="directive">BrowserMatchNoCase</code> directive is
123    semantically identical to the <code class="directive"><a href="#browsermatch">BrowserMatch</a></code> directive.
124    However, it provides for case-insensitive matching. For
125    example:</p>
126<pre class="prettyprint lang-config">BrowserMatchNoCase mac platform=macintosh
127BrowserMatchNoCase win platform=windows</pre>
128
129
130    <p>The <code class="directive">BrowserMatch</code> and
131    <code class="directive">BrowserMatchNoCase</code> directives are special cases of
132    the <code class="directive"><a href="#setenvif">SetEnvIf</a></code> and <code class="directive"><a href="#setenvifnocase">SetEnvIfNoCase</a></code>
133    directives. The following two lines have the same effect:</p>
134<pre class="prettyprint lang-config">BrowserMatchNoCase Robot is_a_robot
135SetEnvIfNoCase User-Agent Robot is_a_robot</pre>
136
137
138</div>
139<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
140<div class="directive-section"><h2><a name="SetEnvIf" id="SetEnvIf">SetEnvIf</a> <a name="setenvif" id="setenvif">Directive</a></h2>
141<table class="directive">
142<tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables based on attributes of the request
143</td></tr>
144<tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>SetEnvIf <em>attribute
145    regex [!]env-variable</em>[=<em>value</em>]
146    [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
147<tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
148<tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
149<tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
150<tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
151</table>
152    <p>The <code class="directive">SetEnvIf</code> directive defines
153    environment variables based on attributes of the request. The
154    <em>attribute</em> specified in the first argument can be one of four
155    things:</p>
156
157<ol>
158<li>An HTTP request header field (see <a href="http://www.rfc-editor.org/rfc/rfc2616.txt">RFC2616</a>
159    for more information about these); for example: <code>Host</code>,
160    <code>User-Agent</code>, <code>Referer</code>, and
161    <code>Accept-Language</code>.  A regular expression may be
162    used to specify a set of request headers.</li>
163
164<li>One of the following aspects of the request:
165    <ul>
166      <li><code>Remote_Host</code> - the hostname (if available) of
167      the client making the request</li>
168
169      <li><code>Remote_Addr</code> - the IP address of the client
170      making the request</li>
171
172      <li><code>Server_Addr</code> - the IP address of the server
173      on which the request was received (only with versions later
174      than 2.0.43)</li>
175
176      <li><code>Request_Method</code> - the name of the method
177      being used (<code>GET</code>, <code>POST</code>, <em>et
178      cetera</em>)</li>
179
180      <li><code>Request_Protocol</code> - the name and version of
181      the protocol with which the request was made (<em>e.g.</em>,
182      "HTTP/0.9", "HTTP/1.1", <em>etc.</em>)</li>
183
184      <li><code>Request_URI</code> - the resource requested on the HTTP
185       request line -- generally the portion of the URL
186      following the scheme and host portion without the query string. See
187      the <code class="directive"><a href="/mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>
188      directive of <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> for extra information on
189      how to match your query string.</li>
190    </ul>
191</li>
192
193<li>The name of an environment variable in the list of those
194associated with the request. This allows
195<code class="directive">SetEnvIf</code> directives to test against the result
196of prior matches. Only those environment variables defined by earlier
197<code>SetEnvIf[NoCase]</code> directives are available for testing in
198this manner. 'Earlier' means that they were defined at a broader scope
199(such as server-wide) or previously in the current directive's scope.
200Environment variables will be considered only if there was no match
201among request characteristics and a regular expression was not
202used for the <em>attribute</em>.</li>
203
204</ol>
205
206<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>
207matches against the <em>attribute</em>, then the remainder of the
208arguments are evaluated.</p>
209
210<p>The rest of the arguments give the names of variables to set, and
211optionally values to which they should be set. These take the form
212of</p>
213
214    <ol>
215      <li><code><em>varname</em></code>, or</li>
216
217      <li><code>!<em>varname</em></code>, or</li>
218
219      <li><code><em>varname</em>=<em>value</em></code></li>
220    </ol>
221
222    <p>In the first form, the value will be set to "1". The second
223    will remove the given variable if already defined, and the
224    third will set the variable to the literal value given by
225    <code><em>value</em></code>. Since version 2.0.51, Apache httpd will
226    recognize occurrences of <code>$1</code>..<code>$9</code> within
227    <var>value</var> and replace them by parenthesized subexpressions
228    of <var>regex</var>.</p>
229
230<pre class="prettyprint lang-config">SetEnvIf Request_URI "\.gif$" object_is_image=gif
231SetEnvIf Request_URI "\.jpg$" object_is_image=jpg
232SetEnvIf Request_URI "\.xbm$" object_is_image=xbm
233    
234SetEnvIf Referer www\.mydomain\.example\.com intra_site_referral
235    
236SetEnvIf object_is_image xbm XBIT_PROCESSING=1
237    
238SetEnvIf ^TS  ^[a-z]  HAVE_TS</pre>
239
240
241    <p>The first three will set the environment variable
242    <code>object_is_image</code> if the request was for an image
243    file, and the fourth sets <code>intra_site_referral</code> if
244    the referring page was somewhere on the
245    <code>www.mydomain.example.com</code> Web site.</p>
246
247    <p>The last example will set environment variable
248    <code>HAVE_TS</code> if the request contains any headers that
249    begin with "TS" whose values begins with any character in the
250    set [a-z].</p>
251
252<h3>See also</h3>
253<ul>
254<li><a href="/env.html">Environment Variables in Apache HTTP Server</a>,
255for additional examples.
256</li>
257</ul>
258</div>
259<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
260<div class="directive-section"><h2><a name="SetEnvIfExpr" id="SetEnvIfExpr">SetEnvIfExpr</a> <a name="setenvifexpr" id="setenvifexpr">Directive</a></h2>
261<table class="directive">
262<tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables based on an ap_expr expression</td></tr>
263<tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>SetEnvIfExpr <em>expr
264    [!]env-variable</em>[=<em>value</em>]
265    [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
266<tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
267<tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
268<tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
269<tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
270</table>
271    <p>The <code class="directive">SetEnvIfExpr</code> directive defines
272    environment variables based on an <code class="directive"><a href="/mod/core.html#if">&lt;If&gt;</a></code>
273    <code>ap_expr</code>. These expressions will be evaluated at runtime,
274    and applied <em>env-variable</em> in the same fashion as <code class="directive">SetEnvIf</code>.</p>
275
276<pre class="prettyprint lang-config">SetEnvIfExpr "tolower(req('X-Sendfile')) == 'd:\images\very_big.iso')" iso_delivered</pre>
277
278
279    <p>This would set the environment variable <code>iso_delivered</code>
280    every time our application attempts to send it via <code>X-Sendfile</code></p>
281
282    <p>A more useful example would be to set the variable rfc1918 if the
283    remote IP address is a private address according to RFC 1918:</p>
284
285<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>
286
287
288<h3>See also</h3>
289<ul>
290<li><a href="/expr.html">Expressions in Apache HTTP Server</a>,
291for a complete reference and more examples.</li>
292<li><code class="directive"><a href="/mod/core.html#if">&lt;If&gt;</a></code> can be used to achieve similar
293results.</li>
294<li><code class="module"><a href="/mod/mod_filter.html">mod_filter</a></code></li>
295</ul>
296</div>
297<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
298<div class="directive-section"><h2><a name="SetEnvIfNoCase" id="SetEnvIfNoCase">SetEnvIfNoCase</a> <a name="setenvifnocase" id="setenvifnocase">Directive</a></h2>
299<table class="directive">
300<tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets environment variables based on attributes of the request
301without respect to case</td></tr>
302<tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>SetEnvIfNoCase <em>attribute regex
303        [!]env-variable</em>[=<em>value</em>]
304    [[!]<em>env-variable</em>[=<em>value</em>]] ...</code></td></tr>
305<tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
306<tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr>
307<tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Base</td></tr>
308<tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_setenvif</td></tr>
309</table>
310
311    <p>The <code class="directive">SetEnvIfNoCase</code> is semantically identical to
312    the <code class="directive"><a href="#setenvif">SetEnvIf</a></code> directive,
313    and differs only in that the regular expression matching is
314    performed in a case-insensitive manner. For example:</p>
315<pre class="prettyprint lang-config">SetEnvIfNoCase Host Example\.Org site=example</pre>
316
317
318    <p>This will cause the <code>site</code> environment variable
319    to be set to "<code>example</code>" if the HTTP request header
320    field <code>Host:</code> was included and contained
321    <code>Example.Org</code>, <code>example.org</code>, or any other
322    combination.</p>
323
324</div>
325</div>
326<div class="bottomlang">
327<p><span>Available Languages: </span><a href="/en/mod/mod_setenvif.html" title="English">&nbsp;en&nbsp;</a> |
328<a href="/fr/mod/mod_setenvif.html" hreflang="fr" rel="alternate" title="Fran�ais">&nbsp;fr&nbsp;</a> |
329<a href="/ja/mod/mod_setenvif.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
330<a href="/ko/mod/mod_setenvif.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
331<a href="/tr/mod/mod_setenvif.html" hreflang="tr" rel="alternate" title="T�rk�e">&nbsp;tr&nbsp;</a></p>
332</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&amp;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>
333<script type="text/javascript"><!--//--><![CDATA[//><!--
334var comments_shortname = 'httpd';
335var comments_identifier = 'http://httpd.apache.org/docs/2.4/mod/mod_setenvif.html';
336(function(w, d) {
337    if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
338        d.write('<div id="comments_thread"><\/div>');
339        var s = d.createElement('script');
340        s.type = 'text/javascript';
341        s.async = true;
342        s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
343        (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
344    }
345    else { 
346        d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
347    }
348})(window, document);
349//--><!]]></script></div><div id="footer">
350<p class="apache">Copyright 2014 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>
351<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.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[//><!--
352if (typeof(prettyPrint) !== 'undefined') {
353    prettyPrint();
354}
355//--><!]]></script>
356</body></html>