Purpose

As you build more complex rewrite rules, you may find yourself in a situation where debugging is required. The rewrite log will allow you to examine the process in which mod_rewrite examines the requested URI, and applies the requested patterns.

Configuration

apache HTTP server 2.4

Logging for mod_rewrite is now in the ErrorLog, see logging

apache HTTP server 2.2 (and older)

Place the following directives in your vhost, if applicable. Otherwise, place them in the server context:

RewriteEngine on

RewriteLog /path/to/log

RewriteLogLevel 5

Using a RewriteLogLevel value higher than 5 will not produce additional logging output

Rewrite rules in the server context can still apply to specific vhosts if "RewriteEngine on" and "RewriteOptions inherit" is set.

Recipes

Here is a recipe for creating a rewrite log that is actually readable. It is based on Rich Bowen's presentation Apache Nuts and Bolts

Config:

RewriteLogLevel 9
RewriteLog "||/usr/local/rewrite_log_pipe"

Like in a usual setup, we set RewriteLogLevel high enough to be verbose enough. However, instead of logging to a file, we will pipe the log through a program.

This program will be a short and simple Perl script:

#!/usr/bin/env perl

$|++
open (F, ">>/tmp/rewrite.log");
select F;

while (<>) {
    s/^.*(\(\d\).*)/$1/;
    print;
}

This program opens the file /tmp/rewrite.log, looks for anything in the input that looks like (1) or (2) and drops everything before that.

Before:

94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f7ef8/initial] (2) [perdir /srv/www/vhosts/hc-profi/] rewrite 'favicon.ico' -> '/index.php'
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f7ef8/initial] (2) [perdir /srv/www/vhosts/hc-profi/] trying to replace prefix /srv/www/vhosts/hc-profi/ with /
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f7ef8/initial] (1) [perdir /srv/www/vhosts/hc-profi/] internal redirect with /index.php [INTERNAL REDIRECT]
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f60a8/initial/redir#1] (2) init rewrite engine with requested uri /index.php
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f60a8/initial/redir#1] (1) pass through /index.php
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f60a8/initial/redir#1] (3) [perdir /srv/www/vhosts/hc-profi/] strip per-dir prefix: /srv/www/vhosts/hc-profi/index.php -> index.php
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f60a8/initial/redir#1] (3) [perdir /srv/www/vhosts/hc-profi/] applying pattern '^test_.*$' to uri 'index.php'
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f60a8/initial/redir#1] (3) [perdir /srv/www/vhosts/hc-profi/] strip per-dir prefix: /srv/www/vhosts/hc-profi/index.php -> index.php
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f60a8/initial/redir#1] (3) [perdir /srv/www/vhosts/hc-profi/] applying pattern '^index\.php$' to uri 'index.php'
94.62.148.237 - - [22/May/2012:04:05:50 +0300] [94.62.148.237/sid#7f65cc4cbac0][rid#7f65cc7f60a8/initial/redir#1] (1) [perdir /srv/www/vhosts/hc-profi/] pass through /srv/www/vhosts/hc-profi/index.php

After:

(2) [perdir /srv/www/vhosts/hc-profi/] rewrite 'favicon.ico' -> '/index.php'
(2) [perdir /srv/www/vhosts/hc-profi/] trying to replace prefix /srv/www/vhosts/hc-profi/ with /
(1) [perdir /srv/www/vhosts/hc-profi/] internal redirect with /index.php [INTERNAL REDIRECT]
(2) init rewrite engine with requested uri /index.php
(1) pass through /index.php
(3) [perdir /srv/www/vhosts/hc-profi/] strip per-dir prefix: /srv/www/vhosts/hc-profi/index.php -> index.php
(3) [perdir /srv/www/vhosts/hc-profi/] applying pattern '^test_.*$' to uri 'index.php'
(3) [perdir /srv/www/vhosts/hc-profi/] strip per-dir prefix: /srv/www/vhosts/hc-profi/index.php -> index.php
(3) [perdir /srv/www/vhosts/hc-profi/] applying pattern '^index\.php$' to uri 'index.php'
(1) [perdir /srv/www/vhosts/hc-profi/] pass through /srv/www/vhosts/hc-profi/index.php

RewriteLog (last edited 2014-03-27 16:19:45 by thumbs)