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 achieved using the ErrorLog directive, see logging to configure the log level. A value of trace5 is recommended.

apache HTTP server 2.2 (and older)

By editing your vhost

Place the following directives in your vhost, if defined in your configuration. 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 and using a lower level will hide important information.

By editing the Server context

Rewrite rules in the server context can still apply to specific vhosts with the following directives in each vhost:

RewriteEngine on

RewriteOptions inherit

The following directives must be placed in the Server context, in the same fashion as editing your vhost:

RewriteEngine on

RewriteLog /path/to/log

RewriteLogLevel 5

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:

RewriteLog "||/usr/local/rewrite_log_pipe"

Instead of logging to a file, the output will be piped through a script:

#!/usr/bin/env perl

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

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

This script 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

In this example, 'favicon.ico' is rewritten to /index.php, then the '^test_.*$' pattern is applied to the sub-request.

Lastly, the same sub-request has the '^index\.php$' pattern applied to. In this case, the result was a non-match, and the rule is skipped.

RewriteLog (last edited 2014-06-18 23:36:31 by thumbs)