Conditional SetEnv in .htaccess?
Asked Answered
P

2

21

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.

Pileup answered 23/5, 2012 at 20:57 Comment(0)
A
53

For conditional settings there is SetEnvIf:

SetEnvIf Host ^stage\.example\.com$ PYRO_ENV=stage
SetEnvIf Host ^(www\.)?example\.com$ PYRO_ENV=production
Arevalo answered 16/10, 2012 at 0:38 Comment(2)
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.Cavity
This is the best answer.Leidaleiden
M
24

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]
Moises answered 23/5, 2012 at 21:7 Comment(1)
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 and Environment Variables in ApachePileup

© 2022 - 2024 — McMap. All rights reserved.