Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to set a variable on Apache

ENV=DEV if the http host is the dev URL


ENV=PRD if the http host is the prd URL

and then use $_SERVER['ENV'] to create some logic branches

So far none of these have worked for me and we do have the setenvif module installed

SetEnvIfNoCase Referer
SetEnvIfNoCase Remote_Host

What's the right way to do this?

share|improve this question
up vote 11 down vote accepted

Taking a quick look at the documentation:

The SetEnvIf directive defines environment variables based on attributes of the request. The attribute specified in the first argument can be one of three things:

An HTTP request header field (see RFC2616 for more information about these); for example: Host, User-Agent, Referer, and Accept-Language. A regular expression may be used to specify a set of request headers.

So, it's certainly possible to make an environment variable conditional on the Host header. It looks like you're trying to use either the Referer or Remote_Host headers, neither of which is exactly what you want (although in theory Referer should contain the value of the Host header in most cases). Remote_Host would be the hostname of the client making the request, which is not at all what you want (and in most configurations would simply not be available, since it's typical to have DNS lookups turned off for performance reasons).

Try something like this:

SetEnvIf Host "" ENV=DEV
SetEnvIf Host "" ENV=PRD

...and then use $_SERVER['ENV'] to create some logic branches

But note also that you can simply reference the value of the Host header directly in PHP without going through this chicanery; $_SERVER['HTTP_HOST'] will have exactly what you want.

share|improve this answer
Beautiful. This let me write custom rules to require passwords when connecting to certain Hosts, but not others (ie, protect the staging box when one VM is cloned onto multiple Amazon EC2 instances, but don't write custom Chef recipes). Very useful, if hard to search for, functionality. – Eirik Feb 25 '15 at 23:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.