When working with Apache configurations on Ubuntu Precise (12.04 LTS) with PHP 5.5, you might encounter the frustrating "500 Internal Server Error" accompanied by the specific message in /var/log/apache2/error.log
:
/var/www/web/.htaccess: <IfModule> not allowed here
This error typically occurs when Apache's configuration doesn't properly allow for conditional module directives in .htaccess
files, despite having seemingly correct permissions.
The problematic .htaccess
file attempts to implement URL rewriting with fallback handling:
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
While adding this to apache.conf
might appear sufficient:
<Directory />
AllowOverride ALL
</Directory>
There are deeper considerations:
- Apache version differences in handling
IfModule
directives - Potential conflicts with other configuration files
- Module loading order issues
For a robust solution, consider these steps:
# 1. Verify module loading
sudo a2enmod rewrite
sudo service apache2 restart
# 2. Implement proper directory configuration
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# 3. Alternative .htaccess syntax (when needed)
<IfDefine MOD_REWRITE>
RewriteEngine On
# Your rules here
</IfDefine>
When troubleshooting:
# Check active configuration
apache2ctl -M | grep rewrite
# Test configuration syntax
apache2ctl -t
# Verify file permissions
ls -la /var/www/web/.htaccess
For complex deployments:
- Consider moving rules to virtual host configuration
- Evaluate using
mod_macro
for reusable configurations - Implement proper error logging with
LogLevel alert rewrite:trace3
When configuring a fresh Ubuntu Precise server with Apache and PHP 5.5, I suddenly encountered a 500 Internal Server Error when accessing my /var/www/
directory. The error log revealed:
/var/www/web/.htaccess: IfModule not allowed here
The problematic .htaccess file contained standard rewrite rules:
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
After some research, I discovered this occurs when Apache's configuration doesn't properly allow .htaccess overrides. The key was checking two configuration aspects:
# Check if mod_rewrite is loaded
apache2ctl -M | grep rewrite
# Verify AllowOverride settings
grep -R "AllowOverride" /etc/apache2/
The solution was adding proper Directory permissions in apache.conf:
<Directory />
AllowOverride ALL
</Directory>
After restarting Apache with:
sudo service apache2 restart
For those running similar setups, consider these extra configurations:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
And verify your virtual host configuration includes:
AccessFileName .htaccess
Always test configuration changes before restarting:
sudo apache2ctl configtest