Error Codes Wiki

Apache mod_rewrite Errors — URL Rewriting, .htaccess, and Redirect Loop Issues

Warningweb server

Overview

Fix Apache mod_rewrite errors including .htaccess not working, redirect loops, RewriteRule syntax errors, and clean URL routing failures.

Key Details

  • mod_rewrite transforms URLs using regular expressions and rewrite rules
  • .htaccess files allow per-directory configuration but must be enabled with AllowOverride All
  • Redirect loops occur when a RewriteRule matches its own output, creating infinite redirects
  • RewriteCond (conditions) and RewriteRule (actions) work together for URL transformation
  • The [L] flag stops processing further rules; [R=301] sends a permanent redirect

Common Causes

  • AllowOverride set to None in Apache config — .htaccess rules are ignored
  • mod_rewrite module not enabled: 'a2enmod rewrite' not run
  • RewriteRule matching its own rewritten URL causing a redirect loop
  • Incorrect regex in RewriteRule not matching the intended URL pattern

Steps

  1. 1Enable mod_rewrite: 'sudo a2enmod rewrite && sudo systemctl restart apache2'
  2. 2Enable .htaccess: set 'AllowOverride All' in the <Directory> block of your VirtualHost config
  3. 3Test rewrite rules: enable RewriteLog (Apache 2.2) or 'LogLevel alert rewrite:trace3' (Apache 2.4) for debugging
  4. 4Fix redirect loops: add 'RewriteCond %{ENV:REDIRECT_STATUS} 200 [OR] RewriteCond %{REQUEST_URI} ^/target [R=0]' to stop re-processing
  5. 5Verify syntax: 'apachectl configtest' or 'apache2ctl -t' to check for configuration errors

Tags

apachemod-rewritehtaccessredirecturl-rewriting

Related Items

More in Web Server

Frequently Asked Questions

Most likely AllowOverride is set to None in the Apache VirtualHost config. Change it to AllowOverride All for the document root directory, then restart Apache.