DevOpsil
Apache
85%
Needs Review

Fix Apache mod_rewrite Not Working

Aareez AsifAareez Asif3 min read

The Symptom

You have rewrite rules in your .htaccess or Apache configuration, but URLs are not being rewritten. Requests either return 404, hit the wrong handler, or the rules are silently ignored.

For example, this rule should rewrite /about to /about.html:

RewriteEngine On
RewriteRule ^about$ about.html [L]

But visiting /about returns a 404 instead of serving about.html.

Root Cause

mod_rewrite failures usually come from one of these issues:

  1. The mod_rewrite module is not loaded.
  2. AllowOverride is set to None, so .htaccess files are completely ignored.
  3. RewriteEngine On is missing in the current context.
  4. The rewrite rule pattern does not match due to leading slashes or incorrect regex.
  5. The rules are in .htaccess but the directory configuration blocks override processing.

Step-by-Step Fix

1. Verify mod_rewrite is loaded

# Debian/Ubuntu
apache2ctl -M | grep rewrite

# RHEL/CentOS
httpd -M | grep rewrite

If there is no output, enable the module:

# Debian/Ubuntu
sudo a2enmod rewrite
sudo systemctl restart apache2

# RHEL/CentOS - add to httpd.conf
echo "LoadModule rewrite_module modules/mod_rewrite.so" | sudo tee -a /etc/httpd/conf.modules.d/00-rewrite.conf
sudo systemctl restart httpd

2. Check AllowOverride

If your rules are in .htaccess, the directory must allow overrides. Open your virtual host config:

sudo nano /etc/apache2/sites-available/000-default.conf

Find the <Directory> block for your document root and set AllowOverride:

<Directory /var/www/html>
    AllowOverride All
    Require all granted
</Directory>

AllowOverride None is the most common reason .htaccess rules are silently ignored. After changing this, restart Apache:

sudo systemctl restart apache2

3. Verify .htaccess is being read

Create a deliberately broken .htaccess to test:

echo "INVALID_DIRECTIVE" > /var/www/html/.htaccess

Visit the site. If you get a 500 Internal Server Error, Apache is reading the file. If the site loads normally, .htaccess is being ignored (go back to step 2).

Remove the test line after verifying:

rm /var/www/html/.htaccess

4. Fix common rule mistakes

Rules in .htaccess do NOT include a leading slash. Rules in a <VirtualHost> or <Directory> block DO include the leading slash:

# In .htaccess - NO leading slash
RewriteRule ^about$ /about.html [L]

# In VirtualHost config - leading slash
RewriteRule ^/about$ /about.html [L]

Always include RewriteEngine On before any rules:

RewriteEngine On
RewriteBase /
RewriteRule ^about$ about.html [L]

5. Enable rewrite logging for debugging

Add to your virtual host configuration:

LogLevel alert rewrite:trace6

Then check the error log:

sudo tail -f /var/log/apache2/error.log

The trace output shows exactly how each rule is evaluated and why it matches or fails. Disable trace logging after debugging as it generates significant output.

6. Test the configuration

sudo apache2ctl configtest
sudo systemctl reload apache2
curl -I http://localhost/about

Prevention Tips

  • Prefer configuration files over .htaccess. Rules in the virtual host config are more performant because Apache does not need to check for .htaccess files on every request.
  • Use RewriteLog during development. Always test rules with trace logging before deploying to production.
  • Test rules with curl -I or curl -v. Check response headers and status codes to verify rewrites are working as expected.
  • Document your rewrite rules. Add comments explaining what each rule does and why, especially for complex regex patterns.
Share:

Was this article helpful?

Aareez Asif
Aareez Asif

Senior Kubernetes Architect

10+ years orchestrating containers in production. Battle-tested opinions on everything from pod scheduling to service mesh. I've seen clusters burn and helped rebuild them better.

Related Articles

More in Apache

View all →

Discussion