13

I want to enable rewrite logging so that I can debug a rewrite rule but adding the RewriteLog directives is causing a 500 error.

Version information:

Ubuntu 14.04

Server version: Apache/2.4.12 (Ubuntu)
Server built:   Feb  4 2015 14:22:06

contents of .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 5

RewriteBase /

RewriteRule ^/wordpress/wp-content/(.*)$ /wp-content/$1 [L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

In the error log I see:

/var/www/path.to/wordpress/.htaccess: Invalid command 'RewriteLog', perhaps misspelled or defined by a module not included in the server configuration
codecowboy
  • 1,287
  • 5
  • 17
  • 31

2 Answers2

20

In addition to having forgotten that logging cannot be configured in .htaccess files, this also turned out to be an Apache version issue. The RewriteLog directive has been replaced in newer versions and adding the following to the Virtualhost configuration enabled the rewrite log for me:

LogLevel alert rewrite:trace3 (can be increased to trace8)

From the manual:

Those familiar with earlier versions of mod_rewrite will no doubt be looking for the RewriteLog and RewriteLogLevel directives. This functionality has been completely replaced by the new per-module logging configuration mentioned above.

To filter out these messages in the error log you can do something like:

tail -f error_log|fgrep '[rewrite:'
codecowboy
  • 1,287
  • 5
  • 17
  • 31
  • Your question was originally tagged with [tag:apache-2.2] right? and I may have overlooked the version in the question itself. Well spotted. – HBruijn Feb 27 '15 at 06:24
  • In case this helps anyone, my issue was that I was enabling logging in the VHost for port 80 but not 443. I was using HTTPS, so my logging didn't work. Took me a while to find that stupid mistake :( – Mageician Sep 06 '19 at 17:57
  • I'd like to add that you have to have `mod_log_debug` enabled... – thoni56 Dec 17 '19 at 09:57
4

I can't help repeating myself:

Most people who use .htaccess and ask about it on ServerFault should not be using .htaccess in the first place as that is an end-user solution never intended for administrators:

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.
Source: Apache manual

Apparently most people who publish their configurations appear to be cargo cult programmers keen on copying .htaccess files blindly without understanding the reasons.

Your problem is a prime example: the RewriteLog directive is only valid in the context of a server config or the virtual host...

That means it is not allowed in a .htaccess file!

HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • 5
    Looking for logs is exactly what someone who wants to learn, not copy, would do. Seems Apache has set up a classic catch-22: You should only use .htaccess if you *can't* access the server root. But you can only turn on logging if you *can* access the server root. If they let you do all this rewriting in .htaccess, they should let you log what it's doing so you don't have to rely on random copy and paste/trial and error. – Mark Berry May 12 '15 at 19:42
  • 6
    Some applications like Wordpress generate .htaccess files that need to be debugged. This appears to be the case here. Logging from an .htaccess file is a logical first step when you are trying to work out someone else's inscrutable regular expressions. – KeithL Nov 04 '15 at 18:51