2719N/A<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2719N/A <
TITLE>Apache module mod_setenvif</
TITLE>
2719N/A<!-- Background white, links blue (unvisited), navy (visited), red (active) --> 2719N/A <
H1 ALIGN="CENTER">Module mod_setenvif</
H1>
2719N/A This module provides the ability to set environment variables based
2719N/A upon attributes of the request.
2719N/A><
STRONG>Status:</
STRONG></
A> Base
2719N/A><
STRONG>Module Identifier:</
STRONG></
A> setenvif_module
2719N/A><
STRONG>Compatibility:</
STRONG></
A> Available in Apache 1.3 and later.
2719N/A The <
SAMP>mod_setenvif</
SAMP> module allows you to set environment
2719N/A variables according to whether different aspects of the request match
2719N/A you specify. These environment variables can be used by
2719N/A other parts of the server to make decisions about actions to be taken.
2719N/A <
P>The directives are considered in the order they appear in the
2719N/A configuration files. So more complex sequences can be used, such
2719N/A as this example, which sets <
CODE>netscape</
CODE> if the browser
2719N/A BrowserMatch ^Mozilla netscape
2719N/A BrowserMatch MSIE !netscape
2719N/A <
p>For additional information, we proved a document on
2719N/A <
LI><
A HREF="#BrowserMatch">BrowserMatch</
A>
2719N/A <
LI><
A HREF="#BrowserMatchNoCase">BrowserMatchNoCase</
A>
2719N/A <
LI><
A HREF="#SetEnvIf">SetEnvIf</
A>
2719N/A <
LI><
A HREF="#SetEnvIfNoCase">SetEnvIfNoCase</
A>
2719N/A <
HR>
<!-- the HR is part of the directive description --> 2719N/A <
H2><
A NAME="BrowserMatch">BrowserMatch directive</
A></
H2>
2719N/A ><
STRONG>Syntax:</
STRONG></
A> BrowserMatch <
EM>regex
2719N/A envar</
em>[=<
em>value</
em>] [<
em>envar</
em>[=<
em>value</
em>]] ...
2719N/A ><
STRONG>Default:</
STRONG></
A> <
i>none</
i>
2719N/A ><
STRONG>Context:</
STRONG></
A> server config, virtual host, directory,
2719N/A ><
STRONG>Override:</
STRONG></
A> FileInfo
2719N/A ><
STRONG>Status:</
STRONG></
A> Base
2719N/A ><
STRONG>Module:</
STRONG></
A> mod_setenvif
2719N/A ><
STRONG>Compatibility:</
STRONG></
A> Apache 1.2 and above (in Apache 1.2
2719N/A this directive was found in the now-obsolete mod_browser module)
2719N/A The BrowserMatch directive defines environment variables based on the
2719N/A <
SAMP>User-Agent</
SAMP> HTTP request header field. The first argument
2719N/A should be a POSIX.2 extended regular expression (similar to an
2719N/A <
SAMP>egrep</
SAMP>-style regex). The rest of the arguments give the
2719N/A names of variables to set, and optionally values to which they should
2719N/A be set. These take the form of
2719N/A <
LI><
SAMP><
EM>varname</
EM></
SAMP>, or
2719N/A <
LI><
SAMP>!<
EM>varname</
EM></
SAMP>, or
2719N/A <
LI><
SAMP><
EM>varname</
EM>=<
EM>value</
EM></
SAMP>
2719N/A In the first form, the value will be set to "1". The second
2719N/A will remove the given variable if already defined, and the third will
2719N/A set the variable to the value given by <
SAMP><
EM>value</
EM></
SAMP>. If a
2719N/A <
SAMP>User-Agent</
SAMP> string matches more than one entry, they will
2719N/A be merged. Entries are processed in the order in which they appear,
2719N/A and later entries can override earlier ones.
BrowserMatch ^Mozilla forms jpeg=yes browser=netscape
BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript
BrowserMatch MSIE !javascript
Note that the regular expression string is
<
STRONG>case-sensitive</
STRONG>. For case-INsensitive matching, see
HREF="#BrowserMatchNoCase" ><
SAMP>BrowserMatchNoCase</
SAMP></
A>
The <
SAMP>BrowserMatch</
SAMP> and <
SAMP>BrowserMatchNoCase</
SAMP>
directives are special cases of the
><
SAMP>SetEnvIf</
SAMP></
A>
><
SAMP>SetEnvIfNoCase</
SAMP></
A>
directives. The following two lines have the same effect:
BrowserMatchNoCase Robot is_a_robot
SetEnvIfNoCase User-Agent Robot is_a_robot
<
HR>
<!-- the HR is part of the directive description --> <
A NAME="BrowserMatchNoCase">BrowserMatchNoCase directive
><
STRONG>Syntax:</
STRONG></
A> BrowserMatchNoCase <
EM>regex
envar</
em>[=<
em>value</
em>] [<
em>envar</
em>[=<
em>value</
em>]] ...
><
STRONG>Default:</
STRONG></
A> <
EM>none</
EM>
><
STRONG>Context:</
STRONG></
A> server config, virtual host, directory,
><
STRONG>Override:</
STRONG></
A> FileInfo
><
STRONG>Status:</
STRONG></
A> Base
><
STRONG>Module:</
STRONG></
A> mod_setenvif
><
STRONG>Compatibility:</
STRONG></
A> Apache 1.2 and above (in Apache 1.2
this directive was found in the now-obsolete mod_browser module)
The <
SAMP>BrowserMatchNoCase</
SAMP> directive is semantically identical to
><
SAMP>BrowserMatch</
SAMP></
A>
directive. However, it provides for case-insensitive matching. For
BrowserMatchNoCase mac platform=macintosh
BrowserMatchNoCase win platform=windows
The <
SAMP>BrowserMatch</
SAMP> and <
SAMP>BrowserMatchNoCase</
SAMP>
directives are special cases of the
><
SAMP>SetEnvIf</
SAMP></
A>
><
SAMP>SetEnvIfNoCase</
SAMP></
A>
directives. The following two lines have the same effect:
BrowserMatchNoCase Robot is_a_robot
SetEnvIfNoCase User-Agent Robot is_a_robot
<
HR>
<!-- the HR is part of the directive description --> <
A NAME="SetEnvIf">SetEnvIf directive
><
STRONG>Syntax:</
STRONG></
A> SetEnvIf <
EM> attribute regex
envar</
em>[=<
em>value</
em>] [<
em>envar</
em>[=<
em>value</
em>]] ...
><
STRONG>Default:</
STRONG></
A> <
EM>none</
EM>
><
STRONG>Context:</
STRONG></
A> server config, virtual host, directory,
><
STRONG>Override:</
STRONG></
A> FileInfo
><
STRONG>Status:</
STRONG></
A> Base
><
STRONG>Module:</
STRONG></
A> mod_setenvif
><
STRONG>Compatibility:</
STRONG></
A> Apache 1.3 and above; the
Request_Protocol keyword and environment-variable matching are only
available with 1.3.7 and later
The <
SAMP>SetEnvIf</
SAMP> directive defines environment variables
based on attributes of the request. These attributes can be the
values of various HTTP request header fields (see
for more information about these), or of other aspects of the request,
<
LI><
SAMP>Remote_Host</
SAMP> - the hostname (if available) of the
client making the request
<
LI><
SAMP>Remote_Addr</
SAMP> - the IP address of the client making
<
LI><
SAMP>Remote_User</
SAMP> - the authenticated username (if
<
LI><
SAMP>Request_Method</
SAMP> - the name of the method being used
(<
SAMP>GET</
SAMP>, <
SAMP>POST</
SAMP>, <
EM>et cetera</
EM>)
<
LI><
SAMP>Request_Protocol</
SAMP> - the name and version of the protocol
<
LI><
SAMP>Request_URI</
SAMP> - the portion of the URL following the
Some of the more commonly used request header field names include
<
SAMP>Host</
SAMP>, <
SAMP>User-Agent</
SAMP>, and <
SAMP>Referer</
SAMP>.
If the <
EM>attribute</
EM> name doesn't match any of the special keywords,
nor any of the request's header field names, it is tested as the name
of an environment variable in the list of those associated with the request.
This allows <
CODE>SetEnvIf</
CODE> directives to test against the result
<
STRONG>Only those environment variables defined by earlier
<
CODE>SetEnvIf[NoCase]</
CODE> directives are available for testing in
this manner. 'Earlier' means that they were defined at a broader scope
(such as server-wide) or previously in the current directive's
<
EM>attribute</
EM> may be a regular expression when used to match a request header.
If <
EM>attribute</
EM> is a regular expression and it doesn't match any of the
request's header names, then <
EM>attribute</
EM> is not tested against the request's
environment variable list.
SetEnvIf Request_URI "\.gif$" object_is_image=gif
SetEnvIf Request_URI "\.jpg$" object_is_image=jpg
SetEnvIf Request_URI "\.xbm$" object_is_image=xbm
SetEnvIf Referer www\.mydomain\.com intra_site_referral
SetEnvIf object_is_image xbm XBIT_PROCESSING=1
SetEnvIf ^TS* ^[a-z].* HAVE_TS
The first three will set the environment variable <
SAMP>object_is_image</
SAMP> if the
request was for an image file, and the fourth sets
<
SAMP>intra_site_referral</
SAMP> if the referring page was somewhere
The last example will set environment variable <
SAMP>HAVE_TS</
SAMP> if the request
contains any headers that begin with "TS" whose values begins with any character
<
HR>
<!-- the HR is part of the directive description --> <
A NAME="SetEnvIfNoCase">SetEnvIfNoCase directive
><
STRONG>Syntax:</
STRONG></
A> SetEnvIfNoCase <
EM> attribute regex
envar</
em>[=<
em>value</
em>] [<
em>envar</
em>[=<
em>value</
em>]] ...
><
STRONG>Default:</
STRONG></
A> <
EM>none</
EM>
><
STRONG>Context:</
STRONG></
A> server config, virtual host, directory,
><
STRONG>Override:</
STRONG></
A> FileInfo
><
STRONG>Status:</
STRONG></
A> Base
><
STRONG>Module:</
STRONG></
A> mod_setenvif
><
STRONG>Compatibility:</
STRONG></
A> Apache 1.3 and above
The <
SAMP>SetEnvIfNoCase</
SAMP> is semantically identical to the
><
SAMP>SetEnvIf</
SAMP></
A>
directive, and differs only in that the regular expression matching is
performed in a case-insensitive manner. For example:
SetEnvIfNoCase Host Apache\.Org site=apache
This will cause the <
SAMP>site</
SAMP> environment variable to be set to
"<
SAMP>apache</
SAMP>" if the HTTP request header field
<
SAMP>Host:</
SAMP> was included and contained <
SAMP>
Apache.Org</
SAMP>,
<
SAMP>
apache.org</
SAMP>, or any other combination.