Skip to content

How to Fix Apache mod_rewrite Rule Error

DodaTech Updated 2026-06-24 2 min read

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 L flag to stop processing after a match
  • Use R=301 for permanent redirects, R=302 for temporary
  • Test rewrites with R=302 first before changing to R=301
  • Add RewriteCond to prevent loops when rewriting to existing paths

Common Mistakes with mod rewrite

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. 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

### Why does my Apache RewriteRule not match anything?

RewriteRule patterns in .htaccess and <Directory> sections are applied to the relative filesystem path, not the full URL path. Remove the leading slash from the pattern: use ^old-page$ instead of ^/old-page$ when the rule is in .htaccess.

How do I debug Apache mod_rewrite issues?

Enable rewrite logging: RewriteLog "/var/log/apache2/rewrite.log" and set RewriteLogLevel 9. This logs every rewrite attempt with the matched pattern, conditions, and result. Note: RewriteLog is deprecated in Apache 2.4; use LogLevel alert rewrite:trace5 instead.

What is the difference between R=301 and R=302 in mod_rewrite?

R=301 is a permanent redirect (browsers cache it), and R=302 is a temporary redirect (not cached). Use 301 only when the old URL will never be used again. Use 302 during development or for temporary redirects to avoid browser Caching issues.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro