Home >Web >.htaccess and Apache >Debugging .htaccess and the server configuration

Debugging .htaccess and the server configuration

Error 500, an endless redirect loop or a rule that does nothing. How to read the messages in the Apache log, how to find out the values of variables even without access to the log, how to turn on mod_rewrite tracing and how to move the rules to the server configuration.

Messages in the error log

With error 500, the cause is in the server error log. On hosting it is usually in the control panel, in a local environment in the logs folder of the Apache server. Common messages:

  • Invalid command '…', perhaps misspelled or defined by a module not included in the server configuration – a typo in the name of a directive or a module the server does not have. <IfModule> helps (see How .htaccess works);
  • … not allowed here – the directive must not be in .htaccess or AllowOverride does not allow it;
  • Too many arguments to directive, … takes one argument and similar – a wrong number of parameters, often because of a comment after the directive;
  • RewriteRule: bad flag delimiters – wrongly written flags, for example a space instead of a comma in [R=301 L];
  • AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error – an internal rewrite went into a loop (below);
  • AH01630: client denied by server configuration – the request was denied by Require. This is not error 500 but 403, the message only says why.

When the log is not available, halving helps: comment out half of the file, and if the error disappears, it is in the commented-out part. Then keep halving that part.

Loops

An internal loop occurs when the rewritten address matches the same rule again:

RewriteEngine On
RewriteRule ^(.*)$ /version2/$1 [L]

The address /contact/ is rewritten to /version2/contact/, that one to /version2/version2/contact/… After ten rewrites Apache ends with error 500 and the AH00124 message. The rule lacks a condition that skips an already rewritten address:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/version2/
RewriteRule ^(.*)$ /version2/$1 [L]

A redirect loop runs between the browser and the server: a rule redirects to an address that redirects again. After several steps the browser gives up and reports an error (Chrome with the code ERR_TOO_MANY_REDIRECTS). Examples are in the articles Redirects in .htaccess (HTTPS behind another server, two domains in one folder) and mod_rewrite and clean URLs (an old address with a parameter). The individual steps are shown by curl -sIL or by the Network tab in the developer tools with the option turned on that keeps the record when moving to another page (Preserve log in Chrome).

Values of variables without access to the log

When a rule does not work and it is not clear why, it helps to find out which values it works with. A temporary rule redirects to an address in which you can see the values of the variables:

RewriteEngine On
RewriteRule ^debug$ /?host=%{HTTP_HOST}&https=%{HTTPS}&uri=%{REQUEST_URI} [R=302,L]

The address /debug redirects, for example, to /?host=www.example.com&https=on&uri=/debug. Just open it in the browser or use curl -I and read the Location header. The 302 code makes sure that the browser does not store the redirect. Delete the rule afterwards.

Tracing mod_rewrite

A detailed record of how mod_rewrite processes the rules is turned on in the server configuration (in the httpd.conf file or in the configuration of the site, not in .htaccess) by the LogLevel directive:

LogLevel warn rewrite:trace3

It cannot be used in .htaccess – it ends with error 500 and the message LogLevel not allowed here. On hosting without access to the configuration it is therefore not possible, in a local environment it is (Apache is restarted after the change). For each request, the error log then shows which path was compared with which pattern and how the address changed – for example (shortened):

[perdir /var/www/example.com/] strip per-dir prefix: /var/www/example.com/old -> old
[perdir /var/www/example.com/] applying pattern '^old$' to uri 'old'
[perdir /var/www/example.com/] rewrite 'old' -> '/new/'
[perdir /var/www/example.com/] explicitly forcing redirect with https://www.example.com/new/

The first line shows that a pattern in .htaccess sees the path without the folder and without the leading slash. Do not leave tracing turned on on a live server, the log grows quickly.

Rules in the server configuration

Those who manage their own server can put the rules directly into the configuration of the site and turn .htaccess files off. Apache then does not have to look for and read them on every request and the rules are in one place:

<VirtualHost *:443>
  ServerName www.example.com
  DocumentRoot "/var/www/example.com"
  <Directory "/var/www/example.com">
    AllowOverride None
    Require all granted
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
  </Directory>
</VirtualHost>

The certificate settings (SSLEngine, SSLCertificateFile) are left out of the example. Inside <Directory> the rules work the same as in .htaccess. Directly in <VirtualHost> (outside <Directory>), however, the pattern gets the whole path including the leading slash – the rule RewriteRule ^old$ /new/ from .htaccess must read RewriteRule ^/old$ /new/ there.

Unlike .htaccess, changes in the configuration take effect only after the server reloads it. Check the notation first – an error in the configuration would stop the whole server:

apachectl configtest

When the notation is correct, it prints Syntax OK. On Windows, the same is run as httpd -t in the bin folder of the Apache server. On servers with Debian and Ubuntu, the configuration is then reloaded with the command systemctl reload apache2; elsewhere the service is called httpd.

Related articles:
Error messages on a live site
How .htaccess works
mod_rewrite and clean URLs
Redirects in .htaccess: 301, 302 and one address for the site