Declaring variable in .htaccess to determine deployment environment

apache-2.2

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.

i.e.

  • 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.

Best Answer

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?