New Bookstore! Save 20% on books with discount code: LAUNCH
Web Dev + WordPress + Security

Roll your own Apache Rewrite Log

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 :)

Jeff Starr
About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
WP Themes In Depth: Build and sell awesome WordPress themes.

10 responses to “Roll your own Apache Rewrite Log”

  1. Don’t know which version of apache you use, but apache 2.2 does not allow rewritelog in .htaccess files.

  2. Jeff Starr
    Perishable 2008/04/30 7:59 am

    Still using Apache 1.3.41 on a shared host, and hopefully will continue to do so for a long time — don’t know what I’d do without direct control over my Apache rewrite logs. Interesting indeed.. I wonder why Apache decided to make the change. Security or performance? In any case, I certainly appreciate you bringing this to my attention. I will update the article with a note explaining this information. Thank you, jc!

  3. Finally, I’ve found the reason of the server error 500! LOL! Thanks jc!

    @Jeff: I love your site! Thank you for creating it! ;)

  4. Jeff Starr
    Jeff Starr 2009/02/01 8:58 pm

    My pleasure, Aldo! Thanks for the feedback! :)

  5. RewriteLog only works at the server level, outside any directory constructs or .htaccess files.

  6. Tihomir Ipotaliev 2010/12/22 3:05 am

    How to do rewrite log file on local host (Windows)?

  7. I’d just like to say that RewriteLog should NEVER be used in a .htaccess file as it will throw an error 500. Keep it to your vhosts rather..

  8. can’t get it to work at all in 2.2 the directives are outside of VirtualHosts, even inside it doesn;t work… They’re not in .htaccess….

  9. just figured it out lol…

    If you have vhosts, by default mod_rewrite won’t be inherited from the main context. You can fix that by putting:

    RewriteEngine On
    RewriteOptions Inherit

    .. in the vhost, but really you’ll be better off putting your rules in
    the vhost.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Blackhole Pro: Trap bad bots in a virtual black hole.
Thoughts
Take a screenshot with Firefox (no extension required). Open Developer Tools Settings and enable the “Take a screenshot” button. Then click the button :)
Take a screenshot with Chrome (no extension required). Open DevTools, type Cmd + Shift + P, then type screenshot.
After 10 years working on my 2010 iMac, my upgrade finally arrived. Shiny new iMac shipped from Ireland :)
Too much caffeine weirds me out. But I love the taste of coffee. So once in a while I enjoy a small cup of decaf. Hits the spot.
Chris Coyier is a truly awesome person. One of the finest people I've ever worked with. Just #gottasayit
Excel won't open CSV file because SYLK format? Open it with text editor and add an apostrophe ' at the beginning of the file, save changes, done.
Displaying too many social media buttons and links all over the place imho makes you look desperate and frankly kinda sad.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.