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 want to set some environment only for a specific IP,how to do that in Apache?

share|improve this question

See the SetEnvIf directive, which defines environment variables based on attributes of the request, such as Remote_Addr (the IP address of the client making the request)...

SetEnvIf Remote_Addr ^192\.168\.0\. LOCAL_LAN=1

...or Server_Addr (the IP address of the server on which the request was received)...

SetEnvIf Server_Addr ^192\.168\.0\.1$ SITE_PROFILE=PRODUCTION
SetEnvIf Server_Addr ^192\.168\.0\.2$ SITE_PROFILE=DEVELOPMENT

Note that the second argument to SetEnvIf is a regular expression, so be extra mindful if you're tempted to use bare IPs, as a pattern like (while looking a lot cleaner than escaping dots as I do above) will also end up matching


SetEnvIf can't access QUERY_STRING, but per the docs:-

See the RewriteCond directive of mod_rewrite for extra information on how to match your query string.

So something like...

RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.
RewriteCond %{QUERY_STRING} (^|&)debug=
RewriteRule . - [E=LOCAL_LAN_AND_DEBUG:1]
share|improve this answer
How can I do it if there's a debug parameter in the querystring? – linux Jul 14 '11 at 3:42
Per the docs, you would need to use mod_rewrite instead. Example added to answer above. – Kanji Jul 14 '11 at 4:23
What does . - mean in . - [E=LOCAL_LAN_AND_DEBUG:1] ? Also does LOCAL_LAN_AND_DEBUG:1 set both LOCAL_LAN and DEBUG to 1? – linux Jul 14 '11 at 4:51
BTW,isn't querystring also stored in environment variables? – linux Jul 14 '11 at 7:17
I just tried,and the environment variable is NOT set though.. – linux Jul 14 '11 at 9:24

In Apache 2.4 you can set an environment variable for a single IP address or a whole subnet using CIDR notation. Eg.:

<If "-R ''">
  SetEnv myvar1=example

<If "-R ''">
  SetEnv myvar2=subnet

These can also be combined with logical operators inside the expression:

<If "-R '' || 
     -R ''">
  SetEnv myvar3=both
share|improve this answer

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.