23

I'm trying to add a conditional statement using Apache's If directive in my .htaccess file.

I have referenced this page http://httpd.apache.org/docs/trunk/mod/core.html#if but it doesn't elaborate much / give many examples. Two incomplete examples it gives are:

<If %{REQUEST_METHOD} IN GET,HEAD,OPTIONS> 

and

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

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

<If %{SERVER_PORT} IN GET,HEAD,OPTIONS>
   #nothing here yet
</If>

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?

Thanks!

cwd
  • 2,693
  • 9
  • 32
  • 47
  • 1
    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 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

2 Answers2

21

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

http://httpd.apache.org/docs/2.4/mod/core.html#if

Documentation not present in 2.2:

http://httpd.apache.org/docs/2.2/mod/core.html#if

Hawken
  • 285
  • 1
  • 2
  • 12
tgriesser
  • 2,662
  • 2
  • 16
  • 10
20

<If> is only available in Apache 2.4+, so firstly make sure that you have that version.

Examples

# Compare the host name to example.com and redirect to www.example.com if it matches
<If "%{HTTP_HOST} == 'example.com'">
  Redirect permanent "/" "http://www.example.com/"
</If>

<If "%{HTTP_HOST} =~ /regex/">
  SecFilterEngine Off
  SecFilterScanPOST Off
</If>

<If "%{REQUEST_URI} =~ m#/regex/including/slashes/#">
  SecFilterEngine Off
  SecFilterScanPOST Off
</If>

Alternatives to <If>

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>
       ...
    </IfDefine>
    

Example:

# Set environment variable if we're on staging site
SetEnvIf Host staging ROBOTS_NOINDEX

# Set environment variable if we're within a specific folder
SetEnvIf Request_URI ^/app/webroot/files/ ROBOTS_NOINDEX

# Send custom header if environment variable is set
Header set X-Robots-Tag "noindex" ENV=ROBOTS_NOINDEX
Simon East
  • 1,484
  • 1
  • 14
  • 18
  • 2
    Test but not work! in the document from apache, `` directive only parse at startup and the `parameter-name` is only can passing by command promp using `httpd -Dx=y` – William Leung Oct 31 '16 at 11:20
  • Your "Alternatives to If" were working fine for me, nice example. – Kyborek Jul 25 '17 at 07:16