We write a lot of code to htaccess but what is the best method to debug other than refreshing the page ?

Is there some way that I can write it out to a file ? or is there some echo/print function ?

Simply how do i know what my $1 $2 $3 is ?

  • As of 12/24/20 all the answers deal with rewrite rules, rather than logging a string in order to debug an .htaccess file in general. And if I add an invalid command, such as "got here" to my .htaccess file, I get a 500 HTTP error logged telling me that "got" is an invalid command, but with no indication even as to the line number of the invalid command. And no, there is no "echo" or "write a string to a file" Apache directive. Dec 24, 2020 at 19:28

4 Answers 4


You could try the method mentioned in blog post titled A Couple Ways to Debug mod_rewrite (WaybackMachine copy):

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=302,L,QSA]
  • I would suggest making 302 redirect to avoid troubles with cached 301 result in a browser after finishing a test.
    – IPSUS
    Mar 28, 2019 at 14:03
  • The URL in this post is gone; from the rest of the post: what is vardump? And what does the above snipped have to do with headers?
    – sdbbs
    Oct 18, 2021 at 11:59
  • To test this (without having to reload the URL in the browser every time), I'm calling curl -s -i http://example.com/test-url | head -n10
    – hargobind
    Mar 3, 2022 at 9:29

Try these:

RewriteLog "/myfolder/mylogfile.log" 
RewriteLogLevel 3

These are just Regular Expressions with some additions, so you can use Regex Coach for initial testing against URLs, or any other Regex debugging tools.

Cheers! :)

  • 1
    Very useful tool, but remember to remove it after debugging as your can get a massive log file!
    – Coops
    Nov 24, 2009 at 10:22
  • 7
    Apparently this doesn't work in .htaccess files. It has to go in httpd.conf which means it's usually not achievable on shared hosting accounts. :-(
    – Simon East
    Jul 2, 2013 at 4:31

Here is an interesting little hack to "echo" out variables from an .htaccess file.

If you have AllowOverride set to FileInfo you can set and trigger a custom error response in your .htaccess file with the desired variables in the output:

ErrorDocument 404 "Request: %{THE_REQUEST} Referrer: %{HTTP_REFERER} Host: %{HTTP_HOST}"
RewriteRule ^ - [L,R=404]

Depending on how creative you are with expressions you can output quite alot of useful information!

You are not limited to using the 404 status on your "echoed" content. You can even override the status 200 "ErrorDocument"--which coupled with <If> directives could make for some other pretty interesting uses of this hack to return content directly from an .htaccess file.

  • Doesn't work for me. Please explain "AllowOverride set to FileInfo". Getting debugging output from .htaccess files would be SO useful! Nov 15, 2022 at 23:28
  • @DavidSpector AllowOverride is an Apache configuration directive that can be set within <Directory> sections found in Apache configuration files like httpd.conf or apache2.conf. I've updated the answer to link to the documentation on AllowOverride.
    – PeterA
    Nov 18, 2022 at 1:32
  • Still doesn't work for me. It's an error handler for 404, so how can it work if the htaccess file processes an existing file? If this code works, I need an explanation of exactly how and why it works. I find URL rewriting very confusing and just need a way to log or display information then exit out of the htaccess file cleanly. This is, in my view, a minimum need for debugging rewriting rules, especially since Define will not work in an htaccess file. Nov 19, 2022 at 13:31

Perhaps the best way to debug rewrite rules is not to use rewrite rules at all, but to defer URL processing from the htaccess file to a PHP file (let's call it router.php). Then, you can use PHP to do any manipulating you like, with proper error detection and the usual ways to do debugging. This even runs faster, too, since you don't have to use the rewriting module.

To transfer control immediately from .htaccess to router.php, just put the following line in .htaccess:

FallbackResource router.php

Yes, it's really that easy. And yes, it really works. Give it a try. Note that you may have to handle some error conditions in router.php instead of relying on ErrorDocument directives.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .