mod_setenvif.html revision 1974a700e8a7afa11e008101503b014213aa9c17
286N/A<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
286N/A<HTML>
286N/A <HEAD>
286N/A <TITLE>Apache module mod_setenvif</TITLE>
286N/A </HEAD>
286N/A<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
286N/A <BODY
286N/A BGCOLOR="#FFFFFF"
286N/A TEXT="#000000"
286N/A LINK="#0000FF"
286N/A VLINK="#000080"
286N/A ALINK="#FF0000"
286N/A >
286N/A<!--#include virtual="header.html" -->
286N/A <H1 ALIGN="CENTER">Module mod_setenvif</H1>
286N/A <P>
286N/A This module provides the ability to set environment variables based
286N/A upon attributes of the request.
286N/A </P>
286N/A
286N/A<P><A
286N/AHREF="module-dict.html#Status"
286N/AREL="Help"
286N/A><STRONG>Status:</STRONG></A> Base
286N/A<BR>
286N/A<A
286N/AHREF="module-dict.html#SourceFile"
286N/AREL="Help"
286N/A><STRONG>Source File:</STRONG></A> mod_setenvif.c
286N/A<BR>
286N/A<A
286N/AHREF="module-dict.html#ModuleIdentifier"
286N/AREL="Help"
286N/A><STRONG>Module Identifier:</STRONG></A> setenvif_module
286N/A<BR>
286N/A<A
286N/AHREF="module-dict.html#Compatibility"
286N/AREL="Help"
286N/A><STRONG>Compatibility:</STRONG></A> Available in Apache 1.3 and later.
286N/A</P>
286N/A
286N/A <H2>Summary</H2>
286N/A <P>
286N/A The <SAMP>mod_setenvif</SAMP> module allows you to set environment
286N/A variables according to whether different aspects of the request match
286N/A regular expressions you specify. These envariables can be used by
286N/A other parts of the server to make decisions about actions to be taken.
286N/A </P>
286N/A <P>The directives are considered in the order they appear in the
286N/A configuration files. So more complex sequences can be used, such
286N/A as this example, which sets <CODE>netscape</CODE> if the browser
286N/A is mozilla but not MSIE.
286N/A <BLOCKQUOTE><PRE>
286N/A BrowserMatch ^Mozilla netscape
286N/A BrowserMatch MSIE !netscape
286N/A </PRE></BLOCKQUOTE>
286N/A </P>
286N/A
286N/A <H2>Directives</H2>
286N/A <UL>
286N/A <LI><A HREF="#BrowserMatch">BrowserMatch</A>
286N/A </LI>
286N/A <LI><A HREF="#BrowserMatchNoCase">BrowserMatchNoCase</A>
286N/A </LI>
286N/A <LI><A HREF="#SetEnvIf">SetEnvIf</A>
286N/A </LI>
286N/A <LI><A HREF="#SetEnvIfNoCase">SetEnvIfNoCase</A>
286N/A </LI>
286N/A </UL>
286N/A
286N/A <HR> <!-- the HR is part of the directive description -->
286N/A <H2><A NAME="BrowserMatch">BrowserMatch directive</A></H2>
286N/A <P>
286N/A <A
286N/A HREF="directive-dict.html#Syntax"
286N/A REL="Help"
286N/A ><STRONG>Syntax:</STRONG></A> BrowserMatch <EM>regex envar[=value] [...]</EM>
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Default"
286N/A REL="Help"
286N/A ><STRONG>Default:</STRONG></A> <i>none</i>
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Context"
286N/A REL="Help"
286N/A ><STRONG>Context:</STRONG></A> server config, virtual host, directory,
286N/A .htaccess
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Override"
286N/A REL="Help"
286N/A ><STRONG>Override:</STRONG></A> FileInfo
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Status"
286N/A REL="Help"
286N/A ><STRONG>Status:</STRONG></A> Base
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Module"
286N/A REL="Help"
286N/A ><STRONG>Module:</STRONG></A> mod_setenvif
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Compatibility"
286N/A REL="Help"
286N/A ><STRONG>Compatibility:</STRONG></A> Apache 1.2 and above (in Apache 1.2
286N/A this directive was found in the now-obsolete mod_browser module)
286N/A </P>
286N/A <P>
286N/A The BrowserMatch directive defines environment variables based on the
286N/A <SAMP>User-Agent</SAMP> HTTP request header field. The first argument
286N/A should be a POSIX.2 extended regular expression (similar to an
286N/A <SAMP>egrep</SAMP>-style regex). The rest of the arguments give the
286N/A names of variables to set, and optionally values to which they should
286N/A be set. These take the form of
286N/A </P>
286N/A <OL>
286N/A <LI><SAMP><EM>varname</EM></SAMP>, or
286N/A </LI>
286N/A <LI><SAMP>!<EM>varname</EM></SAMP>, or
286N/A </LI>
286N/A <LI><SAMP><EM>varname</EM>=<EM>value</EM></SAMP>
286N/A </LI>
286N/A </OL>
286N/A <P>
286N/A In the first form, the value will be set to &quot;1&quot;. The second
286N/A will remove the given variable if already defined, and the third will
286N/A set the variable to the value given by <SAMP><EM>value</EM></SAMP>. If a
286N/A <SAMP>User-Agent</SAMP> string matches more than one entry, they will
286N/A be merged. Entries are processed in the order in which they appear,
286N/A and later entries can override earlier ones.
286N/A </P>
286N/A <P>
286N/A For example:
286N/A </P>
286N/A <PRE>
286N/A BrowserMatch ^Mozilla forms jpeg=yes browser=netscape
286N/A BrowserMatch "^Mozilla/[2-3]" tables agif frames javascript
286N/A BrowserMatch MSIE !javascript
286N/A </PRE>
286N/A <P>
286N/A Note that the regular expression string is
286N/A <STRONG>case-sensitive</STRONG>. For cane-INsensitive matching, see
286N/A the
286N/A <A
286N/A HREF="#BrowserMatchNoCase"
286N/A ><SAMP>BrowserMatchNoCase</SAMP></A>
286N/A directive.
286N/A </P>
286N/A <P>
286N/A The <SAMP>BrowserMatch</SAMP> and <SAMP>BrowserMatchNoCase</SAMP>
286N/A directives are special cases of the
286N/A <A
286N/A HREF="#SetEnvIf"
286N/A ><SAMP>SetEnvIf</SAMP></A>
286N/A and
286N/A <A
286N/A HREF="#SetEnvIfNoCase"
286N/A ><SAMP>SetEnvIfNoCase</SAMP></A>
286N/A directives. The following two lines have the same effect:
286N/A </P>
286N/A <PRE>
286N/A BrowserMatchNoCase Robot is_a_robot
286N/A SetEnvIfNoCase User-Agent Robot is_a_robot
286N/A </PRE>
286N/A
286N/A <HR> <!-- the HR is part of the directive description -->
286N/A <H2>
286N/A <A NAME="BrowserMatchNoCase">BrowserMatchNoCase directive
286N/A </A>
286N/A </H2>
286N/A <P>
286N/A <A
286N/A HREF="directive-dict.html#Syntax"
286N/A REL="Help"
286N/A ><STRONG>Syntax:</STRONG></A> BrowserMatchNoCase <EM>regex envar[=value]
286N/A [...]</EM>
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Default"
286N/A REL="Help"
286N/A ><STRONG>Default:</STRONG></A> <EM>none</EM>
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Context"
286N/A REL="Help"
286N/A ><STRONG>Context:</STRONG></A> server config, virtual host, directory,
286N/A .htaccess
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Override"
286N/A REL="Help"
286N/A ><STRONG>Override:</STRONG></A> FileInfo
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Status"
286N/A REL="Help"
286N/A ><STRONG>Status:</STRONG></A> Base
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Module"
286N/A REL="Help"
286N/A ><STRONG>Module:</STRONG></A> mod_setenvif
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Compatibility"
286N/A REL="Help"
286N/A ><STRONG>Compatibility:</STRONG></A> Apache 1.2 and above (in Apache 1.2
286N/A this directive was found in the now-obsolete mod_browser module)
286N/A </P>
286N/A <P>
286N/A The <SAMP>BrowserMatchNoCase</SAMP> directive is semantically identical to
286N/A the
286N/A <A
286N/A HREF="#BrowserMatch"
286N/A ><SAMP>BrowserMatch</SAMP></A>
286N/A directive. However, it provides for case-insensitive matching. For
286N/A example:
286N/A </P>
286N/A <PRE>
286N/A BrowserMatchNoCase mac platform=macintosh
286N/A BrowserMatchNoCase win platform=windows
286N/A </PRE>
286N/A <P>
286N/A The <SAMP>BrowserMatch</SAMP> and <SAMP>BrowserMatchNoCase</SAMP>
286N/A directives are special cases of the
286N/A <A
286N/A HREF="#SetEnvIf"
286N/A ><SAMP>SetEnvIf</SAMP></A>
286N/A and
286N/A <A
286N/A HREF="#SetEnvIfNoCase"
286N/A ><SAMP>SetEnvIfNoCase</SAMP></A>
286N/A directives. The following two lines have the same effect:
286N/A </P>
286N/A <PRE>
286N/A BrowserMatchNoCase Robot is_a_robot
286N/A SetEnvIfNoCase User-Agent Robot is_a_robot
286N/A </PRE>
286N/A
286N/A <HR> <!-- the HR is part of the directive description -->
286N/A <H2>
286N/A <A NAME="SetEnvIf">SetEnvIf directive
286N/A </A>
286N/A </H2>
286N/A <P>
286N/A <A
286N/A HREF="directive-dict.html#Syntax"
286N/A REL="Help"
286N/A ><STRONG>Syntax:</STRONG></A> SetEnvIf <EM> attribute regex envar[=value]
286N/A [...]</EM>
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Default"
286N/A REL="Help"
286N/A ><STRONG>Default:</STRONG></A> <EM>none</EM>
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Context"
286N/A REL="Help"
286N/A ><STRONG>Context:</STRONG></A> server config, virtual host, directory,
286N/A .htaccess
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Override"
286N/A REL="Help"
286N/A ><STRONG>Override:</STRONG></A> FileInfo
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Status"
286N/A REL="Help"
286N/A ><STRONG>Status:</STRONG></A> Base
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Module"
286N/A REL="Help"
286N/A ><STRONG>Module:</STRONG></A> mod_setenvif
286N/A <BR>
286N/A <A
286N/A HREF="directive-dict.html#Compatibility"
286N/A REL="Help"
286N/A ><STRONG>Compatibility:</STRONG></A> Apache 1.3 and above; the
286N/A Request_Protocol keyword and environment-variable matching are only
286N/A available with 1.3.7 and later
286N/A </P>
286N/A <P>
286N/A The <SAMP>SetEnvIf</SAMP> directive defines environment variables
286N/A based on attributes of the request. These attributes can be the
286N/A values of various HTTP request header fields (see
286N/A <a href="http://www.rfc-editor.org/rfc/rfc2616.txt">RFC2616</a>
286N/A for more information about these), or of other aspects of the request,
286N/A including the following:
286N/A </P>
286N/A <UL>
286N/A <LI><SAMP>Remote_Host</SAMP> - the hostname (if available) of the
286N/A client making the request
286N/A </LI>
286N/A <LI><SAMP>Remote_Addr</SAMP> - the IP address of the client making
286N/A the request
286N/A </LI>
286N/A <LI><SAMP>Remote_User</SAMP> - the authenticated username (if
286N/A available)
286N/A </LI>
286N/A <LI><SAMP>Request_Method</SAMP> - the name of the method being used
286N/A (<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>
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
</PRE>
<P>
The first three will set the envariable <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>
<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 envar[=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> envariable 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>