Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug mod_rewrite rules?

This is a case of "ask a question and answer it myself", which I'm posting for the benefit of anyone who has the same problem.

I had some problems debugging a mod_rewrite ruleset in a .htaccess file on a shared server, where I couldn't even access the Apache error logs. I found a neat way to debug them, which is this:

  1. Write a short script that simply prints out it's querystring variables. e.g. in PHP:

    <?='<pre>',htmlentities(print_r($_GET,true)),'</pre>'?>
    

    is all you need.

  2. Let's say you name this script "show.php" and put it in /public_html. Then in your .htaccess file, identify the point in your ruleset that you think might be causing the problem, and insert this rule:

    RewriteRule (.*) /show.php?url=$1 [END]
    

The effect is the same as inserting a PRINT statement in a regular program. It'll let you know that (a) You reached that point in the ruleset, and (b) what the current rewritten URL is.

It's not as flash as a real debugging tool, but it gets the job done.

If you're using Apache <2.3.9, you'll have to use [L] instead of [END]. In that case, something to look out for is that your ruleset should not attempt to rewrite "/show.php" to anything else. If that's a problem, you can fix it by adding this rule at the very top:

RewriteRule ^show.php$ - [L]

...Just remember to remove it when you're done debugging!

like image 312
Doin Avatar asked Oct 18 '11 12:10

Doin


Video Answer


1 Answers

Other possibility:

use this online htaccess tester:

http://htaccess.madewithlove.be/

like image 187
Timothée HENRY Avatar answered Sep 29 '22 07:09

Timothée HENRY