Debug your Apache mod_rewrite Rules!

Apache mod_rewrite is incredibly powerful but it’s also a PITA if you’re trying to do something advanced. Luckily, you can log mod_rewrite’s activities and better understand why your rules aren’t working.

Just add RewriteLogLevel and RewriteLog to your Apache VirtualHost config:

<VirtualHost *:80>
  # ...

  RewriteLogLevel 8
  RewriteLog /var/log/apache2/rewrite.log

  # ...
</VirtualHost>

I suggest log level 8 but you can try lower. Logging rewrites will be SLOW: do not enable in production.

Sample rewrite log output:

[perdir /var/www/local.dev/www/] RewriteCond: input='/small/asdfsadf/avatar.jpg' pattern='^(.*)/(.*)/(.*)' => matched
[perdir /var/www/local.dev/www/] RewriteCond: input='/var/www/local.dev/www/small/avatar.jpg' pattern='-f' => matched
[perdir /var/www/local.dev/www/] rewrite 'small/asdfsadf/avatar.jpg' -> '/small/avatar.jpg'
[perdir /var/www/local.dev/www/] internal redirect with /small/avatar.jpg [INTERNAL REDIRECT]

Details: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog

Leave a Reply

Your email address will not be published. Required fields are marked *