The error occurs when trying to use the <If>
directive in Apache's VirtualHost configuration. The specific error message indicates that Apache doesn't recognize this as a valid command, which typically means either:
- The required module isn't loaded
- The syntax format is incorrect for your Apache version
The <If>
directive requires mod_version
to be enabled in Apache. Check if it's loaded with:
apache2ctl -M | grep version
If not enabled, activate it with:
sudo a2enmod version
sudo systemctl restart apache2
For Apache 2.4+ (which supports the expression syntax), you have several alternatives:
Option 1: Using IfDefine-like syntax
<If "%{HTTPS} != 'on'">
Redirect / https://www.mydomain.com/
</If>
Option 2: Using mod_rewrite (more compatible)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
The exact syntax depends on your Apache version:
- Apache 2.4+: Supports full expressions in
<If>
directives - Apache 2.2: Only supports
<IfVersion>
,<IfModule>
, etc. - Apache 2.0: Very limited conditional logic
Here's a fully functional VirtualHost configuration for HTTPS redirection:
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
DocumentRoot /var/www/html
# Option A: Using If directive (Apache 2.4+)
<If "%{SERVER_PROTOCOL} != 'HTTPS'">
Redirect permanent / https://www.mydomain.com/
</If>
# Option B: Using rewrite (more compatible)
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>
If you still encounter issues:
- Check Apache version:
apache2 -v
- Verify syntax:
apache2ctl configtest
- Inspect error logs:
tail -f /var/log/apache2/error.log
- Test modules:
apache2ctl -M
When working with Apache's VirtualHost configuration, you might encounter the frustrating "Invalid command '<If'
" error. This typically happens when trying to use the <If>
directive in your VirtualHost configuration without having the required module enabled.
The <If>
directive is part of Apache's mod_rewrite
module, specifically introduced in Apache 2.4+. Before this version, you'd need to use different syntax for conditional redirects. Even with Apache 2.4+, you must ensure:
# Check if mod_rewrite is enabled
sudo a2enmod rewrite
sudo systemctl restart apache2
Here are three working approaches depending on your Apache version and needs:
1. For Apache 2.4+ with mod_rewrite
<VirtualHost *:80>
ServerName mydomain.com
<If "%{HTTPS} != 'on'">
Redirect permanent / https://www.mydomain.com/
</If>
</VirtualHost>
2. Using traditional mod_rewrite syntax
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
3. Simple Redirect directive (no conditions)
<VirtualHost *:80>
Redirect permanent / https://www.mydomain.com/
</VirtualHost>
After making changes:
# Test configuration
sudo apachectl configtest
# Restart Apache
sudo systemctl restart apache2
# Check enabled modules
apache2ctl -M | grep rewrite
- Using
SERVER_PROTOCOL
instead ofHTTPS
(more reliable) - Forgetting to enable
mod_rewrite
- Mixing Apache 2.2 and 2.4 syntax
- Not using proper comparison operators (
!=
vsne
)
For high-traffic sites, consider these optimizations:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
</IfModule>
The NE
flag prevents escaping of special characters, and wrapping in <IfModule>
makes the configuration more portable.