Don't use error-handler-404 in lighttpd - it's a bad idea

Sat 21 September 2013

In many places you can find suggestions to use

server.error-handler-404 = "/index.php"

to get nice urls on sites like Wordpress. It's a bad idea! server.error-handler-404 forgets GET values, so after some time you'll find yourself wondering why forms aren't working.

Alternative

Best replacment I found is using a mod_magnet. You can install it from repo

aptittude install lighttpd-mod-magnet

After that remember to turn it on in lighttpd

cd /etc/lighttpd/conf-enabled
ln -s ../conf-available/10-magnet.conf

and configure. I suggest such file:

-- cat /etc/lighttpd/rewrite.lua
-- check if physical.path (path to file on disk) exists
attr = lighty.stat(lighty.env["physical.path"])

if (not attr) then
  -- lighttpd didn't found requested file, we will return /index.php
  lighty.env["uri.path"] = "/index.php"
  lighty.env["physical.rel-path"] = lighty.env["uri.path"]
  lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
end

-- if you need debugging I sugest using something like print(lighty.env["uri.path"])
-- it will print value to to lighttpd error.log

You can turn on your new rewrite by adding

magnet.attract-physical-path-to = ( "/etc/lighttpd/rewrite.lua" )

to your site config. Now just remove server.error-handler-404 and restart your lighttpd!

Warning

One last thing, remember what lighttpd guys wrote on their wiki:

"mod_magnet is executed in the core of Lighty and EVERY long-running operation is blocking ALL connections in the server"

so keep your scripts simple.