Lighttpd and clean URLs with server error handler 404

I was setting up a Textpattern CMS install on a Lighttpd webserver a few days ago and wanted to share some findings.

To achieve clean URLs the server.error-handler-404 directive can be used. Lighttpd doesn’t have the same problems as apache with POST data, so comments etc. will just work with this method. However it has another problem as described in their FAQ:

The environment (which is relevant when using CGI or FastCGI) is quite normal, but observe the following: […] QUERY_STRING is not set, so you have to parse the REQUEST_URI yourself to get it. […]

This affects only very few things, things like category filters which sometimes can use query strings (and probably a few plugins use that method as well). The fix is easy enough though: just add this to a plugin you have (or create a new plugin):

if (@txpinterface == 'public') {
  register_callback("asy_fix_querystring", "pretext");

function asy_fix_querystring($event, $step) {
  if (!strstr($_SERVER['REQUEST_URI'],'?') or !empty($_GET))  return;
  $_SERVER['QUERY_STRING'] = preg_replace('#^.*?\?#','',$_SERVER['REQUEST_URI']);
  parse_str($_SERVER['QUERY_STRING'], $_GET);

That’s it! Now $_GET and $_SERVER['QUERY_STRING'] should work as expected.

By: | Comments: 4


  1. Should have used


    instead of the regular expression…

  2. Ah, I could have.

    Seems there is really a function for everything in PHP. :)

  3. Oooh… thanks for this, Sencer. I’ve been using messy URLs on lighttpd, but now I can breathe a clean sigh of relief!

  4. Big, big thanks. This patch is still (lighttpd 1.4.25) up to date.

Commenting has expired for this article.