Summary
The mod_setenvif module allows you to set environment variables if different aspects of the request match regular expressions that you specify. These 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. 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
Module: mod_setenvif | |
Syntax: BrowserMatch regex envar[=value] [...] | |
Default: none | |
Context: server config, virtual host, directory, .htaccess | |
Override: none | |
Origin: Apache | |
Example: BrowserMatch ^Mozilla forms jpeg=yes browser=netscape |
BrowserMatch defines environment variables based on the User-Agent HTTP request header field. The first argument should be a POSIX.2 extended regular expression (similar to an egrep-style regex). The rest of the arguments give the names of variables to set, and optional values to which they should be set. These take the form of the following:
See Environment variables on HTTP Server for more information.
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 a User-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
In the above example, if the User-Agent field is Mozilla, the environment variables forms, jpeg=yes and browser=netscape will be set. If the request is Mozilla/2 or Mozilla/3, then in addition to the environment variables on the first BrowserMatch directive, the variables tables, agif, frames and javascript will be set.
BrowserMatchNoCase Robot is_a_robot SetEnvIfNoCase User-Agent Robot is_a_robot
Module: mod_setenvif | |
Syntax: BrowserMatchNoCase regex envar[=value] [...] | |
Default: none | |
Context: server config, virtual host, directory, .htaccess | |
Override: none | |
Origin: Apache | |
Example: BrowserMatchNoCase ibm platform=ibm |
BrowserMatchNoCase is semantically identical to BrowserMatch. However, it provides for case-insensitive matching. For example:
BrowserMatchNoCase mac platform=ibm BrowserMatchNoCase win platform=windows
BrowserMatch and BrowserMatchNoCase are special cases of SetEnvIf and SetEnvIfNoCase. The following two lines have the same effect:
BrowserMatchNoCase Robot is_a_robot SetEnvIfNoCase User-Agent Robot is_a_robot
Module: mod_setenvif | |
Syntax: SetEnvIf attribute regex envar[=value] [...] | |
Default: none | |
Context: server config, virtual host, directory, .htaccess | |
Override: none | |
Origin: Apache | |
Example: SetEnvIf Request_URI "\.gif$" object_is_image=gif |
SetEnvIf defines environment variables based on attributes of the request. These attributes can be the values of various HTTP request header fields or of other aspects of the request. See RFC2068 for more information.
Attribute | Description |
---|---|
Remote_Host | The hostname (if available) of the client making the request. |
Remote_Addr | The IP address of the client making the request. |
Remote_User | The authenticated username (if available). |
Request_Method | The name of the method being used (GET, POST). |
Request_Protocol | The name of the method being used (GET, POST). |
Request_URI | The portion of the URL following the scheme and host portion. |
Some of the more commonly used request header field names include Host, User-Agent, and Referrer.
If the attribute name does not match any of the special keywords, or 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 in a broader context (such as server-wide) or previously in the current directive's context. For 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 Referrer www\.mydomain\.com intra_site_referral : SetEnvIf object_is_image xbm XBIT_PROCESSING=1
The first three will set the environment variable object_is_image if the request was for an image file, and the fourth sets intra_site_referral if the referring page was somewhere on the www.mydomain.com Web site. The 5th statement of the example sets XBIT processing, if the environment variable object_is_image was set by the directive.
Module: mod_setenvif | |
Syntax: SetEnvIfNoCase attribute regex envar[=value] [...] | |
Default: none | |
Context: server config, virtual host, directory .htaccess | |
Override: none | |
Origin: Apache | |
Example: SetEnvIfNoCase Host IBM\.Org site=ibm |
SetEnvIfNoCase is semantically identical to SetEnvIf, and differs only in that the regular expression matching is performed in a case-insensitive manner. For example:
SetEnvIfNoCase Host QIBM\.Org site=ibm
This will cause the site variable to be set to 'ibm' if the HTTP request header field Host: was included and contained QIBM.Org, qibm.org, or any other combination.