Apache HTTP Server Version 2.0
Apache Module mod_setenvif
Description: Allows the setting of environment variables based on characteristics of the request Status: Base Module Identifier: setenvif_module Compatibility: Available in Apache 1.3 and later Summary
The
mod_setenvif
module allows you to set environment variables according to whether different aspects of the request match regular expressions you specify. These environment variables can be used by other parts of the server to make decisions about actions to be taken.The directives are considered in the order they appear in the configuration files. So more complex sequences can be used, such as this example, which sets
netscape
if the browser is mozilla but not MSIE.
BrowserMatch ^Mozilla netscape
BrowserMatch MSIE !netscape
Directives
See also
BrowserMatch Directive
Description: Sets environment variables conditional on HTTP User-Agent Syntax: BrowserMatch regex env-variable[=value] [env-variable[=value]] ... Context: server config, virtual host, directory, .htaccess Override: FileInfo Status: Base Module: mod_setenvif Compatibility: Apache 1.2 and above (in Apache 1.2 this directive was found in the now-obsolete mod_browser module) The
BrowserMatch
directive defines environment variables based on theUser-Agent
HTTP request header field. The first argument should be a POSIX.2 extended regular expression (similar to anegrep
-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
varname
, or!varname
, orvarname=value
In the first form, the value will be set to "1". The second will remove the given variable if already defined, and the third will set the variable to the value given by
value
. If aUser-Agent
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.For example:
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 case-sensitive. For case-INsensitive matching, see the
BrowserMatchNoCase
directive.The
BrowserMatch
andBrowserMatchNoCase
directives are special cases of theSetEnvIf
andSetEnvIfNoCase
directives. The following two lines have the same effect:
BrowserMatchNoCase Robot is_a_robot
SetEnvIfNoCase User-Agent Robot is_a_robot
BrowserMatchNoCase Directive
Description: Sets environment variables conditional on User-Agent without respect to case Syntax: BrowserMatchNoCase regex env-variable[=value] [env-variable[=value]] ... Context: server config, virtual host, directory, .htaccess Override: FileInfo Status: Base Module: mod_setenvif Compatibility: Apache 1.2 and above (in Apache 1.2 this directive was found in the now-obsolete mod_browser module) The
BrowserMatchNoCase
directive is semantically identical to theBrowserMatch
directive. However, it provides for case-insensitive matching. For example:
BrowserMatchNoCase mac platform=macintosh
BrowserMatchNoCase win platform=windows
The
BrowserMatch
andBrowserMatchNoCase
directives are special cases of theSetEnvIf
andSetEnvIfNoCase
directives. The following two lines have the same effect:
BrowserMatchNoCase Robot is_a_robot
SetEnvIfNoCase User-Agent Robot is_a_robot
SetEnvIf Directive
Description: Sets environment variables based on attributes of the request Syntax: SetEnvIf attribute regex env-variable[=value] [env-variable[=value]] ... Context: server config, virtual host, directory, .htaccess Override: FileInfo Status: Base Module: mod_setenvif Compatibility: Apache 1.3 and above; the Request_Protocol keyword and environment-variable matching are only available with 1.3.7 and later The
SetEnvIf
directive defines environment variables based on attributes of the request. These attributes can be the values of various HTTP request header fields (see RFC2616 for more information about these), or of other aspects of the request, including the following:
Remote_Host
- the hostname (if available) of the client making the requestRemote_Addr
- the IP address of the client making the requestRemote_User
- the authenticated username (if available)Request_Method
- the name of the method being used (GET
,POST
, et cetera)Request_Protocol
- the name and version of the protocol with which the request was made (e.g., "HTTP/0.9", "HTTP/1.1", etc.)Request_URI
- the portion of the URL following the scheme and host portionSome of the more commonly used request header field names include
Host
,User-Agent
, andReferer
.If the attribute 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
SetEnvIf
directives to test against the result of prior matches.
Only those environment variables defined by earlier SetEnvIf[NoCase]
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.attribute may be a regular expression when used to match a request header. If attribute is a regular expression and it doesn't match any of the request's header names, then attribute is not tested against the request's environment variable list.
Example:
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
object_is_image
if the request was for an image file, and the fourth setsintra_site_referral
if the referring page was somewhere on thewww.mydomain.com
Web site.The last example will set environment variable
HAVE_TS
if the request contains any headers that begin with "TS" whose values begins with any character in the set [a-z].SetEnvIfNoCase Directive
Description: Sets environment variables based on attributes of the request without respect to case Syntax: SetEnvIfNoCase attribute regex env-variable[=value] [env-variable[=value]] ... Context: server config, virtual host, directory, .htaccess Override: FileInfo Status: Base Module: mod_setenvif Compatibility: Apache 1.3 and above The
SetEnvIfNoCase
is semantically identical to theSetEnvIf
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
site
environment variable to be set to "apache
" if the HTTP request header fieldHost:
was included and containedApache.Org
,apache.org
, or any other combination.Apache HTTP Server Version 2.0