Debugging apache mod_rewrite rules

By ayates

If you find yourself scratching your head, wondering why that mod_rewrite rule isn't working as you'd expect you can add a logger by simply adding the following to your vhost:

RewriteLog '/var/log/apache2/rewrite_log'
RewriteLogLevel 4

Once added reload apache and it'll start tracking.

Note: Make sure the log file exists and is writable or you'll find apache doesn't come back from the reload.

An example of what you'll see in the log (in my case I'm redirecting to a different robots.txt file when the file exists):

strip per-dir prefix: /var/www/localhost/robots.txt -> robots.txt
applying pattern '^robots\.txt$' to uri 'robots.txt'
RewriteCond: input='localhost.local' pattern='^(.*)$' => matched
RewriteCond: input='/var/www/localhost/robots_localhost.local.txt' pattern='-f' => matched
rewrite 'robots.txt' -> '/robots_localhost.local.txt'
internal redirect with /robots_localhost.local.txt [INTERNAL REDIRECT]