Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 add a conditional statement using Apache's If directive in my .htaccess file.

I have referenced this page but it doesn't elaborate much / give many examples. Two incomplete examples it gives are:



<If "$req{Host} = ''"> 

So I've tried to add this to my .htaccess file:

   #nothing here yet

But I keep getting Error 500 when I try and load the page. This is on my local install, and it was working fine previously (or if I remove that code). I believe I have AllowOverride All set up globally, and the context for the If directive should let it be present in .htaccess ("Context: server config, virtual host, directory, .htaccess").

Can someone give me some examples of how to properly use the <If> directive, or some guidance as to why it is not working for me?


share|improve this question
If you're getting a 500 error, that means that Apache has logged something to your error log. What does it say? Also, your test for SERVER_PORT in GET,HEAD,OPTIONS doesn't make any sense (SERVER_PORT is going to be the number port number on which the request was received). – larsks Feb 22 '11 at 20:14
Thanks, larsks. Looks like I am running apache 2.0 which does not support the <If> directive. Maybe I need to look into upgrading to 2.3. At least this explains why there are no examples anywhere (b/c the feature is so new). Thanks for the advice. – cwd Feb 22 '11 at 20:37
up vote 10 down vote accepted

The <If> directive is only available in Apache 2.4+ and not 2.2 or earlier.

Documentation not present in 2.2:

share|improve this answer

<If> is only available in Apache 2.4+, but there are a few other solutions for doing conditional statements in Apache 2.2 if you're still stuck with that.

  • You can set environment variables via:

    • Apache configuration
    • SetEnvIf
    • mod_rewrite's RewriteMatch
  • Then you can perform conditional statements using

    <IfDefine MyEnvironmentVar>

Will try and post a full working example here at some stage.

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.