Module mod_setenvif

Module mod_setenvif supports directives for the IBM® HTTP Server for i Web server.

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, as well as becoming available to CGI scripts and SSI pages.

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

When the server looks up a path via an internal subrequest such as looking for a DirectoryIndex or generating a directory listing with mod_auto_index, per-request environment variables are not inherited in the subrequest. Additionally, SetEnvIf directives are not separately evaluated in the subrequest due to the API phases mod_setenvif takes action in.

Directives

BrowserMatch

Module: mod_setenvif
Syntax: BrowserMatch regex [!]env-variable[=value] [[!]env-variable[=value]]...
Default: none
Context: server config, virtual host, directory, .htaccess
Override: FileInfo
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 perl compatible regular expression . 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:

  • varname
  • !varname
  • varname=value

See Environment variables set by 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.

Note: The regular expression string is case-sensitive. For case-insensitive matching, see BrowserMatchNoCase. BrowserMatch and BrowserMatchNoCase are special cases of SetEnvIf and SetEnvIfNoCase. The following two lines have the same effect:
BrowserMatch Robot is_a_robot 
SetEnvIf User-Agent Robot is_a_robot
Parameter One: regex
  • The regex parameter is a case-sensitive perl compatible regular expression. This gives the user the ability to select variants on the User-Agent field, such as using some wildcarding to group versions of a client browser. See Environment variables set by HTTP Server for more information.
Parameter Two: [!]env-variable[=value] [[!]env-variable[=value]] ...
  • The [!]env-variable[=value] [[!]env-variable[=value]] ... parameter gives the names of the variables to set and, optional, values to which they should be set. The case is preserved when lowercase characters are specified. Valid values include all EBCDIC characters. The value must be enclosed in quotation marks if it contains any non-alphanumeric character or blanks.

BrowserMatchNoCase

Module: mod_setenvif
Syntax: BrowserMatchNoCase regex [!]env-variable[=value] [[!]env-variable[=value]] ...
Default: none
Context: server config, virtual host, directory, .htaccess
Override: FileInfo
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
Parameter One: regex
  • The regex parameter is a case-insensitive perl compatible regular expression. This gives the user the ability to select variants on the User-Agent field, such as using some wildcarding to group version of a client browser. See Environment variables set by HTTP Server for more information.
Parameter Two: [!]env-variable[=value] [[!]env-variable[=value]] ...
  • The [!]env-variable[=value] [[!]env-variable[=value]] ... parameter gives the names of variables to set and, optionally, values to which they should be set. They can take the form of 'varname', '!varname' or 'varname=value'. The case is preserved when lowercase characters are specified. Valid values include all EBCDIC characters. The value must be enclosed in quotation marks if it contains any non-alphanumeric character or blanks.

SetEnvIf

Module: mod_setenvif
Syntax: SetEnvIf attribute regex [!]env-variable[=value] [[!]env-variable[=value]] ...
Default: none
Context: server config, virtual host, directory, .htaccess
Override: FileInfo
Origin: Apache
Example: SetEnvIf Request_URI "\.gif$" object_is_image=gif
Example: SetEnvIf Requet_Method "GET" QIBM_CGI_LIBRARY_LIST="MIME;CGIURL;CGILIBL"

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.

Note: To view the RFC listed above, visit the RFC index search engine Link outside Information Center located on the RFC editor Link outside Information Center web site. Search for the RFC number you want to view. The search engine results display the corresponding RFC title, author, date, and status.
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.
Server_Addr The IP address of the server on which the request was received.
Remote_User The authenticated username (if available).
Request_Method The name of the method being used (GET, POST, etc).
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 resource requested on the HTTP request line -- generally the portion of the URL following the scheme and host portion without the query string. See the RewriteCond directive of mod_rewrite for extra information on how to match your query string.

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. Environment variables will be considered only if there was no match among request characteristics and a regular expression was not used for the attribute.

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 Request_URI "\.(.*)$" EXTENSION=$1 

SetEnvIf ^TS  ^[a-z]  HAVE_TS

SetEnvIf  Requet_Method   "GET"   QIBM_CGI_LIBRARY_LIST="MIME;CGIURL;CGILIBL"

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 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]. The sixth statement sets the library list.

Parameter One: attribute
  • The attribute parameter is the attribute of the request, such as an HTTP header value. The attribute can also be an environment variable that was set by an earlier SETENVIF directive.
Parameter Two: regex
  • The regex parameter is a case-sensitive perl compatible regular expression. This gives the user the ability to select variants on the Attribute field, such as using some wildcarding to group related values, and use those to set the environment variables. See Environment variables set by HTTP Server for more information.
Parameter Three: [!]env-variable[=value] [[!]env-variable[=value]] ...
  • The [!]env-variable[=value] [[!]env-variable[=value]] ... parameter gives the names of variables to set and, optionally, values to which they should be set. They take the form of 'varname', '!varname' or 'varname=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 literal value given by value. HTTP Server will recognize occurrences of $1..$9 within value and replace them by parenthesized subexpressions of regex. $0 provides access to the whole string matched by that pattern.
  • The case is preserved when lowercase characters are specified. Valid values include all EBCDIC characters. The value must be enclosed in quotation marks if it contains any non-alphanumeric character or blanks. Lowercase characters for the library names will not work if this directive is used to change the library list. When changing the library list values, the libraries need to be separated by a semicolon.

SetEnvIfExpr

Module: mod_setenvif
Syntax: SetEnvIfExpr expr [!]env-variable[=value] [[!]env-variable[=value]] ...
Default: none
Context: server config, virtual host, directory .htaccess
Override: FileInfo
Origin: Apache
Example: SetEnvIfExpr "tolower(req('X-Sendfile')) == '/home/images/very_big.iso')" iso_delivered

The SetEnvIfExpr directive defines environment variables based on an <If> ap_expr . These expressions will be evaluated at runtime, and applied env-variable in the same fashion as SetEnvIf.

Example:

#Set the variable rfc1918 if the remote IP address is a private address according to RFC 1918

SetEnvIfExpr "-R '10.0.0.0/8' || -R '172.16.0.0/12' || -R '192.168.0.0/16'" rfc1918

Parameter One: expr
  • The expr is an ap_expr expression to be evaluated at runtime. The environment variables is defined if and only if expression evaluates to true.
Parameter Two: [!]env-variable[=value] [[!]env-variable[=value]] ...
  • The [!]env-variable[=value] [[!]env-variable[=value]] ... parameter gives the names of variables to set and, optionally, values to which they should be set. They take the form of 'varname', '!varname' or 'varname=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 literal value given by value. HTTP Server will recognize occurrences of $1..$9 within value and replace them by parenthesized subexpressions of regex.
  • The case is preserved when lowercase characters are specified. Valid values include all EBCDIC characters. The value must be enclosed in quotation marks if it contains any non-alphanumeric character or blanks. Lowercase characters for the library names will not work if this directive is used to change the library list. When changing the library list values, the libraries need to be separated by a semicolon.

SetEnvIfNoCase

Module: mod_setenvif
Syntax: SetEnvIfNoCase attribute regex [!]env-variable[=value] [[!]env-variable[=value]] ...
Default: none
Context: server config, virtual host, directory .htaccess
Override: FileInfo
Origin: Apache
Example: SetEnvIfNoCase Host IBM\.Org site=ibm
Example: SetEnvIfNoCase Request_Method "get" QIBM_CGI_LIBRARY_LIST="mime;cgiurl;cgilibl"

The SetEnvIfNoCase directive 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.

Parameter One: attribute
  • The attribute parameter is the attribute of the request, such as an HTTP Header value. The attribute can also be an environment variable that was set by an earlier setenvif directive.
Parameter Two: regex
  • The regex parameter is a case-sensitive perl compatible regular expression. This gives the user the ability to select variants on the Attribute field, such as using some wildcarding to group related values, and use those to set the environment variables. See Environment variables set by HTTP Server for more information.
Parameter Three: [!]env-variable[=value] [[!]env-variable[=value]] ...
  • The [!]env-variable[=value] [[!]env-variable[=value]] ... parameter gives the names of variables to set and, optionally, values to which they should be set. They take the form of 'varname', '!varname' or 'varname=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 literal value given by value. HTTP Server will recognize occurrences of $1..$9 within value and replace them by parenthesized subexpressions of regex.
  • The case is preserved when lowercase characters are specified. Valid values include all EBCDIC characters. The value must be enclosed in quotation marks if it contains any non-alphanumeric character or blanks. Lowercase characters for the library names will not work if this directive is used to change the library list. When changing the library list values, the libraries need to be separated by a semicolon.