A Couple Ways to Debug mod_rewrite
At the risk of turning this into an Apache fan blog, I have to mention the handy directive I found today. Many webmasters run in to mod_rewrite at one time or another and every one of them will have at least a little trouble with it. I just came across the RewriteLog Directive and corresponding RewriteLogLevel Directive. You're not going to be able to turn these on with shared hosting, they can't be used in .htaccess. To set up a debugging log for mod_rewrite, you need to add them to your httpd.conf somewhere. In my case I'm working on a server at home so I added RewriteLog "/home/rob4/www/logs/rewrite.log" RewriteLogLevel 3 to the appropriate VirtualHost section. After that I restarted apache and now every request generates a bunch of lines in the log file. Every request that a rule could possibly apply to makes some debug info, so don't forget to remove (or comment out) those lines in the conf file and restart Apache when you're done debugging. While I'm on the subject, here's the other method that's helped me debug mod_rewrite on shared hosting. I originally found the tip from jdMorgan on Webmasterworld and tweaked it when I was doing a multisite Drupal install (here's my post from back then). Basically what you do is dump some of the info that mod_rewrite is using back out into the headers then use the Firebug or LiveHTTP Headers extensions in Firefox to watch the headers and read your debug info. In .htaccess use the condition and rule: RewriteCond %{QUERY_STRING} !vardump RewriteRule (.*) http://www.example.com/$1?vardump&thereq=%{THE_REQUEST}&reqhost=%{HTTP_HOST} [R=301,L,QSA] to dump, for example, the THE_REQUEST and HTTP_HOST into request variables. The R=301 does a 301 redirect instead of a rewrite - this is key. A redirect sends information back to the browser but a rewrite happens all inside the server. The QSA keeps the query string (everything after the ? in the URL) and the L tells mod_rewrite to skip the rest of the rules. The RewriteCond line in the example just checks an extra query parameter to stop us from infinitely redirecting in a loop (since the redirect goes back to the same URL). You'll have to tweak the example to suit your needs but you can set it to dump anything that mod_rewrite can see in .htaccess.
Your rating: None Average: 3.2 (45 votes)

That's very useful. I just love Apache!

Thanks so much for posting this. I am debugging a mod_rewrite problem but had forgotten this trick. (Probably a suppressed memory!) It\'s strange the mod_rewrite docs don\'t mention it more prominently.