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.

2 Answers 2

53

For conditional settings there is SetEnvIf:

SetEnvIf Host ^stage\.example\.com$ PYRO_ENV=stage
SetEnvIf Host ^(www\.)?example\.com$ PYRO_ENV=production
Sign up to request clarification or add additional context in comments.

2 Comments

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.
This is the best answer.
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]

1 Comment

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 Apache

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.