Troubleshoot Apache mod_rewrite rules with RewriteLog logging

18 August, 2014   |   Apache

mod_rewrite is one of the most powerful modules of Apache web server and if not used correctly it can cause havoc on your website and harm your search engine rankings. A simple looking rewrite rule goes through a lot of complicated rewrite engine that does the ruleset processing on your rewrite conditions and regular expressions against the requested URL.

There is a directive named RewriteLog that can help us to understand and analyze how all it’s done and we can interpret it to effectively use our rewrite rules and regular expressions in mod_rewrite.

Enabling RewriteLog

Warning: Please note that this directive is only allowed in server configuration file (httpd.conf). That means you need to have a root access to your web server to edit this file that is only available in VPS and dedicated servers. You cannot place this directive in .htaccess file for shared hosting accounts. It is also advisable to disable this directive as soon as you are done troubleshooting with it as it can create very huge log files on disk and a drastic server load on Apache web server.

To enable RewriteLog directive open your httpd.conf file and locate the VirtualHost container for the specified domain on which you want to enable this directive and add the following lines to it.

<VirtualHost 192.168.1.1>
    ...
    RewriteLog "logs/rewrite.txt"
    RewriteLogLevel 9
</VirtualHost>

RewriteLog directive sets the log file name to which the server logs all the rewrite actions and the RewriteLogLevel sets the verbosity level of that log file. If the log file name does not start with “/” then it is assumed to be relative to the Server Root directory. The maximum log level is “9” which will log almost all the rewriting actions and setting it to “0” will simply disable the rewrite logging actions.

After enabling RewriteLog directive, it will start logging all the internal rewrite actions to the specified log file including direct and sub requests. Here are some sample log entries from the log file hosting a WordPress blog.

[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] add path info postfix: C:/wordpress/about -> C:/wordpress/about/
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] strip per-dir prefix: C:/wordpress/about/ -> about/
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] applying pattern '(^blog/downloads/[^/]+$)' to uri 'about/'
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] add path info postfix: C:/wordpress/about -> C:/wordpress/about/
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] strip per-dir prefix: C:/wordpress/about/ -> about/
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (3) [per-dir C:/wordpress/] applying pattern '.' to uri 'about/'
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (4) RewriteCond: input='C:/wordpress/about' pattern='!-f' => matched
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (4) RewriteCond: input='C:/wordpress/about' pattern='!-d' => matched
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (2) [per-dir C:/wordpress/] rewrite about/ -> /index.php
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (2) [per-dir C:/wordpress/] trying to replace prefix C:/wordpress/ with /
[localhost.com/sid#6b6ff8][rid#acd0c8/initial] (1) [per-dir C:/wordpress/] internal redirect with /index.php [INTERNAL REDIRECT]
[localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (3) [per-dir C:/wordpress/] strip per-dir prefix: C:/wordpress/index.php -> index.php
[localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (3) [per-dir C:/wordpress/] applying pattern '(^blog/downloads/[^/]+$)' to uri 'index.php'
[localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (3) [per-dir C:/wordpress/] strip per-dir prefix: C:/wordpress/index.php -> index.php
[localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (3) [per-dir C:/wordpress/] applying pattern '.' to uri 'index.php'
[localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (4) RewriteCond: input='C:/wordpress/index.php' pattern='!-f' => not-matched
[localhost.com/sid#6b6ff8][rid#ad6eb8/initial/redir#1] (1) [per-dir C:/wordpress/] pass through C:/wordpress/index.php

Here you can clearly see how much internal work is been done silently by the mod_rewrite engine for a single file request.

Now you have learnt how to log the internal processing actions of mod_rewrite module with the help of RewriteLog directive I am sure it will help you solve many rewrite rule and condition patterns.

 

Leave a Comment