22

Is it possible to set a SetEnv variable in an .htaccess file differently depending on hostname?

For example, I need my .htaccess file to have the following value:

SetEnv PYRO_ENV production

On production boxes, and...

SetEnv PYRO_ENV stage

On staging boxes. The .htaccess file is version controlled, which is the reason I'm looking for a conditional solution.

Highway of Life
  • 22,803
  • 16
  • 52
  • 80

2 Answers2

53

For conditional settings there is SetEnvIf:

SetEnvIf Host ^stage\.example\.com$ PYRO_ENV=stage
SetEnvIf Host ^(www\.)?example\.com$ PYRO_ENV=production
xhienne
  • 5,738
  • 1
  • 15
  • 34
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
  • So useful for Magento as well: `SetEnvIf Host \.de MAGE_RUN_CODE=de` (in our case we have internal and external URLs: `example.de` as well as `example.de.testing.local`) - so this works for both. – Alex May 22 '13 at 14:33
  • This is the best answer. – Dmitri Pisarev Oct 23 '14 at 07:36
25

You can, with mod_rewrite:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^stage\.domain\.com$
RewriteRule (.*) $1 [E=PYRO_ENV:stage]

RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule (.*) $1 [E=PYRO_ENV:production]
lanzz
  • 42,060
  • 10
  • 89
  • 98
  • 3
    That is awesome. Thank you! For others who may come across this answer in the future, more documentation can be found on setting environment variables here: [ModRewrite Flags Env](http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_e) and [Environment Variables in Apache](http://httpd.apache.org/docs/2.2/env.html) – Highway of Life May 23 '12 at 21:36