Apache SetEnvIf user’s guide

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 SetEnvIf directive defines environment variables based on attributes of the request. The attribute specified in the first argument can be one of three things:

1, The HTTP request header field:

  • Host (From the HTTP request header field)
  • User-Agent
  • Referer
  • Accept-Language

2, One of the following aspects of the request:

  • 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 (only with versions later than 2.0.43)
  • 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.0“, 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.

3, 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.

The SetEnvIf directive can be used in the following contexts in your Apache configuration:

  • In the global Apache configuration. The directive would be valid for the whole server
  • In virtualhost configurations. The directive should be valid only for that specific virtual host.
  • In Directory section between and . The directive should be valid only for a certain directory and its subdirectories)
  • In .htaccess files. AllowOverride FileInfo must be set to make it take effect.

The SetEnvIf directive has the following form:

SetEnvIf attribute regex env-variable

Where attribute is one of the criteria I’ve just mentioned above, and regex is a Perl compatible regular expression.When the environment variables defined, you can use it in your web applications, or refer to in other config directive in httpd.conf. You can get some usage example here at httpd.apache.org.

Share this post

Post Comment