Roll your own Apache Rewrite Log

.htaccess made easy

Roll your own Apache Rewrite log! Rocking your own rewrite log is super-helpful for testing .htaccess rewrite rules, WordPress Permalinks, and much more. All you need is Apache 2.2 (or previous), mod_rewrite enabled (very common on most servers), and access to your server configuration file, http.conf.

Rewrite log via http.conf

Twist one up via the Apache configuration file by placing this code at the foot of your http.conf:

# Roll your own Rewrite log
# Log details via scale of 1 to 9
# 1 = few details, 5 = enough details, 9 = too much detail
RewriteEngine On
RewriteLog "/absolute/path/to/your/wwwroot/public_html/rewrite.log"
RewriteLogLevel 5

Of course, you’ll want to edit the path to match the actual log file you are using. And make sure it’s writable by the server (check its CHMOD/permissions). Then you’re good to go. You can adjust the logging level to whatever is necessary. Note that the higher the number, the harder your server has to work to generate the data. So best advice is to 1) keep the level as low as possible, and 2) only enable logging on a temporary basis, like for troubleshooting, etc.

Update: For Apache 2.4 and beyond, or until they change it again, RewriteLog and RewriteLogLevel have been replaced by LogLevel/trace. Learn more: Apache 2.2 and Apache 2.4 (current).

via .htaccess

..sorry, doesn’t work with .htaccess. Have to enable logging via config or virtual host file.

Logging Example

Here is an example indicating the type of data logged by the RewriteLog directive:

64.246.32.000 - [07/July/2007:07:07:07 - 0700] [example.com/sid#80077333][rid#800b7a33/initial] (2) init rewrite engine with requested uri http://example.com/wp-comments-post.php
64.246.32.000 - [07/July/2007:07:07:07 - 0700] [example.com/sid#80077333][rid#800b7a33/initial] (2) rewrite http://example.com/wp-comments-post.php -> http://64.246.32.000/
64.246.32.000 - [07/July/2007:07:07:07 - 0700] [example.com/sid#80077333][rid#800b7a33/initial] (2) explicitly forcing redirect with http://64.246.32.000/
64.246.32.000 - [07/July/2007:07:07:07 - 0700] [example.com/sid#80077333][rid#800b7a33/initial] (1) escaping http://64.246.32.000/ for redirect
64.246.32.000 - [07/July/2007:07:07:07 - 0700] [example.com/sid#80077333][rid#800b7a33/initial] (1) redirect to http://64.246.32.000/ [REDIRECT/301]

Everything you need to clean up hacked sites and block bad bots :)