How to debug mod_rewrite .htaccess problems with
RewriteLog / Solve mod_rewrite broken redirects
Its common thing that
CMS systems and many developers
custom .htaccess cause issues where websites depending on
mod_rewrite fails to work properly. Most common issues are broken
redirects or mod_rewrite rules, which behave differently among the
different mod_rewrite versions which comes with different versions
of Apache.
Everytime there are such problems its necessery that
mod_rewrite's RewriteLog functionality is used.
Even though the
RewriteLog mod_rewrite config
variable is well described on
httpd.apache.org , I decided
to drop a little post here as I'm pretty sure many novice admins
might not know about
RewriteLog config var and might benefit
of this small article.
Enabling mod_rewrite requests logging of requests to the webserver
and process via mod_rewrite rules is being done either via the
specific website
.htaccess (located in the site's root
directory) or via
httpd.conf, apache2.conf etc. depending on
the Linux / BSD linux distribution Apache config file naming is
used.
To enable RewriteLog near the end of the Apache configuration file
its necessery to place the variables in apache conf:
1. Edit RewriteLog and place following variables:
RewriteLogLevel 9
RewriteLog /var/log/rewrite.log
RewriteLogLevel does define the level of logging that should get
logged in
/var/log/rewrite.log
The higher the
RewriteLogLevel number defined the more
debugging related to mod_rewrite requests processing gets
logged.
RewriteLogLevel 9 is actually the highest loglevel that can
be. Setting the
RewriteLogLevel to 0 will instruct
mod_rewrite to stop logging. In many cases a RewriteLogLevel of 3
is also enough to debug most of the redirect issues, however I
prefer to see more, so almost always I use RewriteLogLevel of
9.
2. Create /var/log/rewrite.log and set writtable
permissions
a. Create /var/log/rewrite.log
freebsd# touch /var/log/rewrite.log
b. Set writtable permissons
Either chown the file to the user with which the Apache server is
running, or chmod it to permissions of 777.
On FreeBSD, chown permissions to allow webserver to write in file,
should be:
freebsd# chown www:www
/var/log/rewrite.log
On Debian and alike distros:
debian:~# chown www-data:www-data
/var/log/rewrite.log
On CentOS, Fedora etc.:
[root@centos ~]# chown httpd:httpd
/var/log/rewrite.log
On any other distribution, you don't want to bother to check the
uid:gid, the permissions can be set with chmod 777, e.g.:
linux# chmod 777 /var/log/rewrite.log
Next after RewriteLog is in conf to make configs active the usual
webserver restart is required.
To restart Apache On FreeBSD:
freebsd# /usr/local/etc/rc.d/apache2 restart
...
To restart Apache on Debian and derivatives:
debian:~# /etc/init.d/apache2 restart
...
On Fedora and derivive distros:
[root@fedora ~]# /etc/init.d/httpd restart
...
Its common error to forget to set proper permissions to
/var/log/rewrite.log this has puzzled me many times, when
enabling RewriteLog's logging.
Another important note is when debugging for mod_rewrite is
enabled, one forgets to disable logging and after a while if the
/var/log partition is placed on a small partition or is on an old
server with less space often the RewriteLog fills in the disk
quickly and might create website downtimes. Hence always make sure
RewriteLog is disabled after work rewrite debugging is no
longer needed.
The way I use to disable it is by commenting it in conf like
so:
#RewriteLogLevel 9
#RewriteLog /var/log/rewrite.log
Finally to check, what the mod_rewrite processor is doing on the
fly its handy to use the well known
tail -f
linux# tail -f /var/log/rewrite.log
A bunch of time in watching the requests, should be enough to point
to the exact problem causing broken redirects or general website
malfunction.
Cheers ;)