How to debug Apache mod_rewrite

ApacheMod Rewrite

Apache Problem Overview


I have two main problems with mod_rewrite:

  1. There is no meaningful error reported when I have an invalid rule

    Enter image description here

  2. To reliably test each modification, I have to erase Google Chrome's cache. This isn't rocket science, but I have to hit Ctrl + Shift + Delete, click OK, and close the window, and reload.

I'd like to see if any of the gurus are willing to share their secrets to efficiently managing mod_rewrite code.

Apache Solutions


Solution 1 - Apache

One trick is to turn on the rewrite log. To turn it on, try this line in your Apache HTTP Server main configuration or current virtual host file (not in .htaccess):

LogLevel alert rewrite:trace6

Before Apache httpd 2.4 mod_rewrite, such a per-module logging configuration did not exist yet. Instead you could use the following logging settings:

RewriteEngine On
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

Solution 2 - Apache

The RewriteLog directive as mentioned by Ben is not available any more in Apache 2.4.

You need to use the LogLevel directive instead. E.g.,

LogLevel alert rewrite:trace6

See Apache Module mod_rewrite, Logging.

Solution 3 - Apache

For basic URL resolution, use a command line fetcher like wget or curl to do the testing, rather than a manual browser. Then you don't have to clear any cache; just up arrow and press Enter in a shell to rerun your test fetches.

Solution 4 - Apache

There's the htaccess tester.

It shows which conditions were tested for a certain URL, which ones met the criteria and which rules got executed.

It seems to have some glitches, though.

Solution 5 - Apache

Based on Ben's answer, you could do the following when running Apache on Linux (Debian in my case).

First create the file rewrite-log.load.

/etc/apache2/mods-availabe/rewrite-log.load

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

Then enter

> $ a2enmod rewrite-log

followed by

> $ service apache2 restart

And when you are finished with debugging your rewrite rules,

> $ a2dismod rewrite-log && service apache2 restart

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpukView Question on Stackoverflow
Solution 1 - ApacheBenView Answer on Stackoverflow
Solution 2 - ApacheTobias SchultzeView Answer on Stackoverflow
Solution 3 - ApacheKazView Answer on Stackoverflow
Solution 4 - ApacheAndyView Answer on Stackoverflow
Solution 5 - ApacheFlowView Answer on Stackoverflow