Target Apache versions with IfVersion

Category: Blog • Posted by Jeff Starr • Updated:

Apache provides a convenient way to conditionally check for its version number, which can be useful when writing directives that require specific modules and configurations. This tutorial explains how it’s done with a few simple examples.

Thanks to Apache’s version module, mod_version, we conditionally may check Apache’s current version number. This module is available in Apache 2.0.56 and later.

Currently the only directive included in mod_version is <IfVersion>, which may be included via the server configuration file, virtual host, directory, or .htaccess. So it’s a pretty straightforward module to learn. Let’s look at some examples of <IfVersion> to see how it works.


Greater than or equal to a specific version of Apache:

<IfVersion >= 2.4>
	# do stuff for Apache greater than or equal to version 2.4.0

Less than specific Apache version:

<IfVersion < 2.3>
	# do stuff for Apache less than version 2.3.0

Using a regular expression to match specific version numbers:

<IfVersion = /^2.4.[01234]$/>
	# matches versions 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4

Instead of forward-slashing the regex as in the previous example, we can replace the slashes with a tilde ~:

<IfVersion !~ ^2.4.[01234]$>
	# matches any version that is NOT 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4

Notice here that we are negating the regular expression by prefixing it with an exclamation point !. Also note that if the operator is omitted, it’s assumed to be “equals” =. You can get more details in the Apache documentation.