How to Fix Apache mod_rewrite Rule Error
In this tutorial, you'll learn about How to Fix Apache mod_rewrite Rule Error. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Apache mod_rewrite rules do not match the expected URLs or create infinite redirect loops — the RewriteRule pattern is incorrect or the RewriteCond conditions are not properly evaluated.
The Problem
# Wrong: Creates infinite redirect loop
RewriteEngine On
RewriteRule ^/old-page$ /new-page [R=301,L]
# Result: /new-page also matches ^/old-page$ pattern in some configurations
Step-by-Step Fix
Step 1: Enable mod_rewrite
sudo a2enmod rewrite
sudo systemctl restart apache2
Step 2: Use proper RewriteCond before RewriteRule
RewriteEngine On
# Only rewrite if the file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite everything to index.php
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Step 3: Debug rewrite rules
RewriteEngine On
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 5
Check the log:
tail -f /var/log/apache2/rewrite.log
Step 4: Fix redirect loop
# Add condition to prevent loop
RewriteCond %{REQUEST_URI} !^/new-page$
RewriteRule ^/old-page$ /new-page [R=301,L]
Step 5: Test rewrite rules
curl -I http://localhost/old-page
Expected: HTTP/1.1 301 Moved Permanently with Location: /new-page
Prevention Tips
- Always add
Lflag to stop processing after a match - Use
R=301for permanent redirects,R=302for temporary - Test rewrites with
R=302first before changing toR=301 - Add RewriteCond to prevent loops when rewriting to existing paths
Common Mistakes with mod rewrite
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world APACHE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro