22
votes

tl;dr: How do I do the following in a .conf or .htaccess file:

<IfApache22>
  # Do A
</IfApache22>
<IfApache24>
  # Do B
</IfApache24>

Longer question:

With Apache 2.4 the old Order get's deprecated in favor of Require.

In my .htaccess files I have

<FilesMatch "\.(long|list|file|types)$">
  Order allow,deny
</FilesMatch>

which means Apache fails to start unless I enable access_compat. While doing so presents a useful workaround, I want a solution that works with both syntaxes as the config will be distributed to a lot of servers. The question is how I can detect the current version of Apache and apply the correct directive.

I intend to use the file for a framework that is distributed to and used by a lot of people, and I can't control/guarantee that they have or lack any particular server setup, which is why I'd like the file to be 2.2/2.4 "agnostic".

5

5 Answers

31
votes

A hack that I have used.

# Apache 2.2
<IfModule !mod_authz_core.c>
    Satisfy Any
</IfModule>

# Apache 2.4
<IfModule mod_authz_core.c>
    Require all granted
</IfModule>
19
votes

Assuming you have mod_version installed (many distributions ship it by default) you can do the following:

<IfVersion >= 2.4>
    # Require ...
</IfVersion>
<IfVersion < 2.4>
    # Order ...
    # Deny ...
    # Allow ...
</IfVersion>
2
votes

You can run different versions of Apache on the same host. Q: What's wrong with separate config files in separate directories? I honestly believe that's probably the cleanest approach...

Nevertheless, Apache .conf files allow an <IfDefine>, which you can specify at runtime with "-D":

http://httpd.apache.org/docs/2.0/mod/core.html#ifdefine

1
votes

You could bypass the need to know by using mod_rewrite for your access control:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(long|list|file|types)$
RewriteRule .* - [F,L]
1
votes

I ran into this problem because FallbackResource is not in early versions of Apache and often clever hosting companies remove mod_rewrite once they have a version of Apache with FallbackResource. I use the following .htaccess when I want to put library code up that adapts to its environment:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^ - [E=protossl]
    RewriteCond %{HTTPS} on
    RewriteRule ^ - [E=protossl:s]
    RewriteRule "(^|/)\." - [F]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^ index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    FallbackResource index.php
</IfModule>

Now of course there are some Apache versions / setups where this fails - but if mod_rewrite is there, use it and if not, hope that FallbackResource is there.