apache Apache mod-rewrite and htaccess related Articles htaccess

How to set an ENV (Environment) variable using RewriteRule in htaccess


Last Updated on April 15, 2021 by Amit

Setting environment variables by htaccess

There are different ways and directives to set an environment variable on Apache server through a htaccess file. One of the most common and the easiest way to set an Environment variable is by using a RewriteRule directive. It’s the easiest method because you can rewrite your domain requests based on env and also you don’t need to write multiple lines of code a one single line of Rule can do this for you.
In this article I will show you how can set the ENV variable using our htaccess file. I will post some code examples to explain the process.

Setting an ENV via mod-rewrite

You can use the mod-rewrite E flag which is short for ENV to set the variable on your server.
The following RewriteRule example sets an ENV variable named foo value bar :

RewriteEngine on

RewriteRule .* - [E:foo=bar]

The rule above will set the ENV foo and assign bar as value to it. The pattern .* matches all URLs meaning that if you visit any URL of your site the ENV will be set. You can change or restrict the pattern to match a specific path only if you want to.
For example the pattern ^demo.php$ will set the ENV only if the /demo.php is requested.

How to check the ENV variable

To check if the variable is set or notset you can match against %{ENV:varName} in a RewriteCond.
The following example checks if the variable “foo” is set and contains bar as value.

    RewriteCond %{ENV:foo} ^bar$

To set the variable if its not already set you can use a RewriteRule

RewriteEngine on

RewriteCond %{ENV:foo} ^$
RewriteRule .* - [E:foo=bar]

The ^$ in condition pattern above checkeks an empty string. If the foo variable is empty/notSer then the rule is triggered and sets the ENV variable.

Setting ENV using mod-env

Apache ENV module provides three different directives to set an environment variable in htaccess and server configuration file.

  • SetEnv directive
  • SetEnvIf directive
  • SetEnvNoCase directive
  • These three directives can be used to set the ENV variable if you don’t want to use a RewriteRule based solution.
    SeEnv directive takes two perameters env_name and env_val .
    The following example sets an environment variable named “foo” and value “bar” :

    SetEnv foo bar

    You can use SetEnvIf directive to set the variable conditionally based on requested URIs and Host headers, for example, If you need to set ENV variable only when the request is for /file.php , you can use the following :

    SetEnvIf request_uri ^/file.php$ foo=bar

    SetEnvNoCase is same as the SetEnvIf except that it ignores the case of characters in pattern, this also matches a request URI string with uppercase characters ie : /FiLe.php .

    SetEnvIfNocase request_uri ^/file.php$ foo=bar

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

By Amit

I am a freelance web developer/designer , blogger and StackOverflow contributer from India.

Leave a Reply

Your email address will not be published. Required fields are marked *