I have the following rule defined in .htaccess file.

RewriteRule ^/home$          http://someothersite.com/home
RewriteRule ^/dir1/dir2/$        http://someothersite.com/dir1/dir2/

I need to set the hostname http://someothersite.com as a .htaccess variable. This variable would change dependant on the deployment environment location. Is it possible to do that?

I have to 3 deployment environments environments: build, dev and stage. For each of the environments, a .htaccess file in build should redirect to 3 external sites.


  • build: the .htaccess file should redirect to build.someothersite.com
  • dev: the .htaccess file should redirect to dev.someothersite.com
  • stage: the .htaccess file should redirect to stage.someothersite.com

Instead of creating 3 different .htaccess files for each of the three different environments I want to create just one .htaccess file without hardcoding the hostname: build.someothersite.com, dev.someothersite.com, stage.someothersite.com.

I need something like this:

RewriteRule ^/home.html =>%{ENV:external_host}/build.html

Is a way I can set a %{ENV:external_host} variable externally in build, dev or stage environment using some SetEnv directive? For example, the variable would set something like the following:

  • SetEnv= dev.someothersite.com in dev,
  • SetEnv= build.someothersite.com
  • SetEnv= stage.someothersite.com in stage respectively.

3 Answers 3


Keep in mind that environment variables are local to the scope of the running user. I.e. the $home would resolve to the user's home folder that apache is running under. i.e. /root or /home/apache or /sbin/nologin or some other version. So, the question is... which variables are you trying to make use of?

  • Hi CompWiz, I have updated my question to make it more clear.
    – tintin
    Sep 5, 2010 at 9:52

Your question doesn't make much sense. It seems you have decided on a solution using "variables." You should state the actual problem. Apache is very flexible and somebody may have a better way of doing it.

Notwithstanding, through mod_rewrite you have access environmental variables using %{ENV:variable} in RewriteCond and you set them using the SetEnv directive. The question becomes how where you going to set the variable? I cannot answer that question unless you say what you mean by "location." If by "location" you mean vhost then you set it in the vhost configuration.

An alternative approach may be using a RewriteMap.

  • Hi embobo, I have updated my question to make it more clear.
    – tintin
    Sep 5, 2010 at 9:52

It sounds like you want to use a conditional variable within Apache. I do this using a shell script to conditionally set a variable in the shell environment, which is then inherited by Apache upon restart.

You can set shell environment variables in /etc/apache2/envvars on Ubuntu, /usr/local/etc/apache22/envvars.d/ on FreeBSD. On CentOS/RHEL, I think the right file is /etc/sysconfig/httpd, but I am not sure.

The HTTPD startup script will source the files when it is invoked. These variables become part of Apache's shell environment and can be referenced within the Apache configuration. Your conditional logic can be as powerful as your want here.

For example, I have something like in /usr/local/etc/apache22/envvars.d/hostname.env:

HOSTNAME=`hostname -s`
if [ "$HOSTNAME" == "foo" ]; then
    export INCLUDE_CONFFILE = default.conf

And then I use this environment variable in my main httpd.conf :

# We have some host-specific settings for some hosts.
# INCLUDE_CONFFILE is inherited from envvars.d/hostname.env
Include etc/apache22/Includes/${INCLUDE_CONFFILE}

These variables should also be readable in .htaccess files.

Note that the operating system environment variables are completely different from the Apache environment variables, so don't confuse the two. The Apache HTTP Server manual says: "Although these variables are referred to as environment variables, they are not the same as the environment variables controlled by the underlying operating system."

  • Hi Stefan, I have updated my question to make it more clear.
    – tintin
    Sep 5, 2010 at 9:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .