Mod_rewrite is Apache web server module that enables you to do behind scenes URL rewrite on your Apache powered web site. Using mod_rewrite you can make your site URL's user friendly and hide your site inner workings to increase security. Writing mod_rewrite rules requires some effort to master regular expressions and somewhat hairy mod_rewrite syntax. The whole process is much easier when you have insight to rewrite engine. In this article I'll show you how to debug Apache mod_rewrite by enabling logging feature while creating your rewrite rules.
Recently I've been working on WordPress PHP caching subsystem for TechyTalk.info. Wile developing this code I had to write some pretty complicated mod_rewrite rules and that would be impossible without insight to mod_rewrite. What I've done to make my development easier was to enable mod_rewrite logging feature. This feature can be enabled by editing your httpd.conf
file or by editing your .htaccess
file for use on per directory basis. Here's an example for Apache lteq 2.2:
<IfModule mod_rewrite.c> RewriteLog "/path/to/you/rewrite/log/file.log" RewriteLogLevel 9 </IfModule> |
If you're using Apache 2.4 directive has changed to:
<IfModule mod_rewrite.c> LogLevel mod_rewrite.c:trace8 </IfModule> |
Logging data can now be found inside global error log file (usually something like /var/log/apache2/error.log).
You can tweak RewriteLogLevel
in range 0 to 9 to control verbosity. Simple as that, the only thing you must make sure is that your Apache web server has write access to your /path/to/you/rewrite/log/file.log
file. Also I wouldn't enable this on production server because any additional logging will decrease mod_rewrite and Apache performance in general.
Ubuntu Linux based operating systems
If you are using Ubuntu Linux based operating system instead of editing /etc/apache2/apache2.conf
file (Ubuntu version of httpd.conf file) you can create new file with your enable logging instructions inside /etc/apache2/conf.d/
directory. This file will be automatically parsed after you restart your apache web server. Also you can place this instructions inside your virtual hosts file if you want to enable logging only for one virtual host. This files are on Ubuntu based distributions stored inside /etc/apache2/sites-available/
directory.