mod_setenvif.html revision 2fbcb47faf267c01a8254429378b444b51d9207b
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens Elkner<HTML>
e6d40133bc9f858308654afb1262b8b483ec5922Till Mossakowski <HEAD>
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder <TITLE>Apache module mod_setenvif</TITLE>
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu </HEAD>
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
3f69b6948966979163bdfe8331c38833d5d90ecdChristian Maeder <BODY
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder BGCOLOR="#FFFFFF"
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder TEXT="#000000"
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder LINK="#0000FF"
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder VLINK="#000080"
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder ALINK="#FF0000"
ce3570dd4d10d200bf1a66323755bb3f4cb0d70cChristian Maeder >
ce3570dd4d10d200bf1a66323755bb3f4cb0d70cChristian Maeder<!--#include virtual="header.html" -->
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder <H1 ALIGN="CENTER">Module mod_setenvif</H1>
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder <P>
54b5e4bca82975ecb0d9591308733f4ee257ccbbChristian Maeder This module provides the ability to set environment variables based
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder upon attributes of the request.
f39493e876547adf5fd9d8c9b094cda58380c17eChristian Maeder </P>
bc6b744cb0be1a69ae95ead472c9c7f0ac6eb7c4cmaeder
be48b0a668c4129700eb2778271ef25c95b6d97bChristian Maeder<P><A
be48b0a668c4129700eb2778271ef25c95b6d97bChristian MaederHREF="module-dict.html#Status"
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian MaederREL="Help"
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder><STRONG>Status:</STRONG></A> Base
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder<BR>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder<A
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian MaederHREF="module-dict.html#SourceFile"
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian MaederREL="Help"
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder><STRONG>Source File:</STRONG></A> mod_setenvif.c
be48b0a668c4129700eb2778271ef25c95b6d97bChristian Maeder<BR>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder<A
275f2a1080c2089e14a1455dbbbb3bc992d8e593cmaederHREF="module-dict.html#ModuleIdentifier"
275f2a1080c2089e14a1455dbbbb3bc992d8e593cmaederREL="Help"
275f2a1080c2089e14a1455dbbbb3bc992d8e593cmaeder><STRONG>Module Identifier:</STRONG></A> setenvif_module
275f2a1080c2089e14a1455dbbbb3bc992d8e593cmaeder<BR>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder<A
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian MaederHREF="module-dict.html#Compatibility"
be48b0a668c4129700eb2778271ef25c95b6d97bChristian MaederREL="Help"
be48b0a668c4129700eb2778271ef25c95b6d97bChristian Maeder><STRONG>Compatibility:</STRONG></A> Available in Apache 1.3 and later.
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder</P>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <H2>Summary</H2>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <P>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder The <SAMP>mod_setenvif</SAMP> module allows you to set environment
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder variables according to whether different aspects of the request
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder match regular expressions you specify. These environment variables
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder can be used by other parts of the server to make decisions about
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder actions to be taken.
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder </P>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <P>The directives are considered in the order they appear in the
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder configuration files. So more complex sequences can be used, such
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder as this example, which sets <CODE>netscape</CODE> if the browser
be48b0a668c4129700eb2778271ef25c95b6d97bChristian Maeder is mozilla but not MSIE.
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <BLOCKQUOTE><PRE>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder BrowserMatch ^Mozilla netscape
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder BrowserMatch MSIE !netscape
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder </PRE></BLOCKQUOTE>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder </P>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <p>For additional information, we provide a document on
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <a href="/env.html">Environment Variables in Apache</a>.</p>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <H2>Directives</H2>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <UL>
b959b3ff2f129f3d88ad43c23bad0e78d05739d1Christian Maeder <LI><A HREF="#BrowserMatch">BrowserMatch</A>
</LI>
<LI><A HREF="#BrowserMatchNoCase">BrowserMatchNoCase</A>
</LI>
<LI><A HREF="#SetEnvIf">SetEnvIf</A>
</LI>
<LI><A HREF="#SetEnvIfNoCase">SetEnvIfNoCase</A>
</LI>
</UL>
<HR> <!-- the HR is part of the directive description -->
<H2><A NAME="BrowserMatch">BrowserMatch directive</A></H2>
<P>
<A
HREF="directive-dict.html#Syntax"
REL="Help"
><STRONG>Syntax:</STRONG></A> BrowserMatch <EM>regex
env-variable</em>[=<em>value</em>] [<em>env-variable</em>[=<em>value</em>]] ...
<BR>
<A
HREF="directive-dict.html#Default"
REL="Help"
><STRONG>Default:</STRONG></A> <i>none</i>
<BR>
<A
HREF="directive-dict.html#Context"
REL="Help"
><STRONG>Context:</STRONG></A> server config, virtual host, directory,
.htaccess
<BR>
<A
HREF="directive-dict.html#Override"
REL="Help"
><STRONG>Override:</STRONG></A> FileInfo
<BR>
<A
HREF="directive-dict.html#Status"
REL="Help"
><STRONG>Status:</STRONG></A> Base
<BR>
<A
HREF="directive-dict.html#Module"
REL="Help"
><STRONG>Module:</STRONG></A> mod_setenvif
<BR>
<A
HREF="directive-dict.html#Compatibility"
REL="Help"
><STRONG>Compatibility:</STRONG></A> Apache 1.2 and above (in Apache 1.2
this directive was found in the now-obsolete mod_browser module)
</P>
<P>
The BrowserMatch directive defines environment variables based on the
<SAMP>User-Agent</SAMP> HTTP request header field. The first argument
should be a POSIX.2 extended regular expression (similar to an
<SAMP>egrep</SAMP>-style regex). The rest of the arguments give the
names of variables to set, and optionally values to which they should
be set. These take the form of
</P>
<OL>
<LI><SAMP><EM>varname</EM></SAMP>, or
</LI>
<LI><SAMP>!<EM>varname</EM></SAMP>, or
</LI>
<LI><SAMP><EM>varname</EM>=<EM>value</EM></SAMP>
</LI>
</OL>
<P>
In the first form, the value will be set to &quot;1&quot;. The second
will remove the given variable if already defined, and the third will
set the variable to the value given by <SAMP><EM>value</EM></SAMP>. If a
<SAMP>User-Agent</SAMP> string matches more than one entry, they will
be merged. Entries are processed in the order in which they appear,
and later entries can override earlier ones.
</P>
<P>
For example:
</P>
<PRE>
BrowserMatch ^Mozilla forms jpeg=yes browser=netscape
BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript
BrowserMatch MSIE !javascript
</PRE>
<P>
Note that the regular expression string is
<STRONG>case-sensitive</STRONG>. For case-INsensitive matching, see
the
<A
HREF="#BrowserMatchNoCase"
><SAMP>BrowserMatchNoCase</SAMP></A>
directive.
</P>
<P>
The <SAMP>BrowserMatch</SAMP> and <SAMP>BrowserMatchNoCase</SAMP>
directives are special cases of the
<A
HREF="#SetEnvIf"
><SAMP>SetEnvIf</SAMP></A>
and
<A
HREF="#SetEnvIfNoCase"
><SAMP>SetEnvIfNoCase</SAMP></A>
directives. The following two lines have the same effect:
</P>
<PRE>
BrowserMatchNoCase Robot is_a_robot
SetEnvIfNoCase User-Agent Robot is_a_robot
</PRE>
<HR> <!-- the HR is part of the directive description -->
<H2>
<A NAME="BrowserMatchNoCase">BrowserMatchNoCase directive
</A>
</H2>
<P>
<A
HREF="directive-dict.html#Syntax"
REL="Help"
><STRONG>Syntax:</STRONG></A> BrowserMatchNoCase <EM>regex
env-variable</em>[=<em>value</em>] [<em>env-variable</em>[=<em>value</em>]] ...
<BR>
<A
HREF="directive-dict.html#Default"
REL="Help"
><STRONG>Default:</STRONG></A> <EM>none</EM>
<BR>
<A
HREF="directive-dict.html#Context"
REL="Help"
><STRONG>Context:</STRONG></A> server config, virtual host, directory,
.htaccess
<BR>
<A
HREF="directive-dict.html#Override"
REL="Help"
><STRONG>Override:</STRONG></A> FileInfo
<BR>
<A
HREF="directive-dict.html#Status"
REL="Help"
><STRONG>Status:</STRONG></A> Base
<BR>
<A
HREF="directive-dict.html#Module"
REL="Help"
><STRONG>Module:</STRONG></A> mod_setenvif
<BR>
<A
HREF="directive-dict.html#Compatibility"
REL="Help"
><STRONG>Compatibility:</STRONG></A> Apache 1.2 and above (in Apache 1.2
this directive was found in the now-obsolete mod_browser module)
</P>
<P>
The <SAMP>BrowserMatchNoCase</SAMP> directive is semantically identical to
the
<A
HREF="#BrowserMatch"
><SAMP>BrowserMatch</SAMP></A>
directive. However, it provides for case-insensitive matching. For
example:
</P>
<PRE>
BrowserMatchNoCase mac platform=macintosh
BrowserMatchNoCase win platform=windows
</PRE>
<P>
The <SAMP>BrowserMatch</SAMP> and <SAMP>BrowserMatchNoCase</SAMP>
directives are special cases of the
<A
HREF="#SetEnvIf"
><SAMP>SetEnvIf</SAMP></A>
and
<A
HREF="#SetEnvIfNoCase"
><SAMP>SetEnvIfNoCase</SAMP></A>
directives. The following two lines have the same effect:
</P>
<PRE>
BrowserMatchNoCase Robot is_a_robot
SetEnvIfNoCase User-Agent Robot is_a_robot
</PRE>
<HR> <!-- the HR is part of the directive description -->
<H2>
<A NAME="SetEnvIf">SetEnvIf directive
</A>
</H2>
<P>
<A
HREF="directive-dict.html#Syntax"
REL="Help"
><STRONG>Syntax:</STRONG></A> SetEnvIf <EM> attribute regex
env-variable</em>[=<em>value</em>] [<em>env-variable</em>[=<em>value</em>]] ...
<BR>
<A
HREF="directive-dict.html#Default"
REL="Help"
><STRONG>Default:</STRONG></A> <EM>none</EM>
<BR>
<A
HREF="directive-dict.html#Context"
REL="Help"
><STRONG>Context:</STRONG></A> server config, virtual host, directory,
.htaccess
<BR>
<A
HREF="directive-dict.html#Override"
REL="Help"
><STRONG>Override:</STRONG></A> FileInfo
<BR>
<A
HREF="directive-dict.html#Status"
REL="Help"
><STRONG>Status:</STRONG></A> Base
<BR>
<A
HREF="directive-dict.html#Module"
REL="Help"
><STRONG>Module:</STRONG></A> mod_setenvif
<BR>
<A
HREF="directive-dict.html#Compatibility"
REL="Help"
><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
</P>
<P>
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
<a href="http://www.rfc-editor.org/rfc/rfc2616.txt">RFC2616</a>
for more information about these), or of other aspects of the request,
including the following:
</P>
<UL>
<LI><SAMP>Remote_Host</SAMP> - the hostname (if available) of the
client making the request
</LI>
<LI><SAMP>Remote_Addr</SAMP> - the IP address of the client making
the request
</LI>
<LI><SAMP>Remote_User</SAMP> - the authenticated username (if
available)
</LI>
<LI><SAMP>Request_Method</SAMP> - the name of the method being used
(<SAMP>GET</SAMP>, <SAMP>POST</SAMP>, <EM>et cetera</EM>)
</LI>
<LI><SAMP>Request_Protocol</SAMP> - the name and version of the protocol
with which the request was made (<EM>e.g.</EM>, "HTTP/0.9", "HTTP/1.1",
<EM>etc.</EM>)
</LI>
<LI><SAMP>Request_URI</SAMP> - the portion of the URL following the
scheme and host portion
</LI>
</UL>
<P>
Some of the more commonly used request header field names include
<SAMP>Host</SAMP>, <SAMP>User-Agent</SAMP>, and <SAMP>Referer</SAMP>.
</P>
<P>
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
of prior matches.
</P>
<BLOCKQUOTE>
<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
scope.</STRONG>
</BLOCKQUOTE>
<P>
<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.
</P>
<P>
Example:
</P>
<PRE>
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
</PRE>
<P>
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
on the <SAMP>www.mydomain.com</SAMP> Web site.
</P>
<P>
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
in the set [a-z].
</P>
<HR> <!-- the HR is part of the directive description -->
<H2>
<A NAME="SetEnvIfNoCase">SetEnvIfNoCase directive
</A>
</H2>
<P>
<A
HREF="directive-dict.html#Syntax"
REL="Help"
><STRONG>Syntax:</STRONG></A> SetEnvIfNoCase <EM> attribute regex
env-variable</em>[=<em>value</em>] [<em>env-variable</em>[=<em>value</em>]] ...
<BR>
<A
HREF="directive-dict.html#Default"
REL="Help"
><STRONG>Default:</STRONG></A> <EM>none</EM>
<BR>
<A
HREF="directive-dict.html#Context"
REL="Help"
><STRONG>Context:</STRONG></A> server config, virtual host, directory,
.htaccess
<BR>
<A
HREF="directive-dict.html#Override"
REL="Help"
><STRONG>Override:</STRONG></A> FileInfo
<BR>
<A
HREF="directive-dict.html#Status"
REL="Help"
><STRONG>Status:</STRONG></A> Base
<BR>
<A
HREF="directive-dict.html#Module"
REL="Help"
><STRONG>Module:</STRONG></A> mod_setenvif
<BR>
<A
HREF="directive-dict.html#Compatibility"
REL="Help"
><STRONG>Compatibility:</STRONG></A> Apache 1.3 and above
</P>
<P>
The <SAMP>SetEnvIfNoCase</SAMP> is semantically identical to the
<A
HREF="#SetEnvIf"
><SAMP>SetEnvIf</SAMP></A>
directive, and differs only in that the regular expression matching is
performed in a case-insensitive manner. For example:
</P>
<PRE>
SetEnvIfNoCase Host Apache\.Org site=apache
</PRE>
<P>
This will cause the <SAMP>site</SAMP> environment variable to be set to
&quot;<SAMP>apache</SAMP>&quot; 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.
</P>
<!--#include virtual="footer.html" -->
</BODY>
</HTML>