by mars on 2007-05-17 3 Comments
filed in Work with tags ,

I've been working with lighttpd's mod_magnet recently to provide Routing & Controller logic to static web sites where PHP is a bit heavy handed (and Rails even more so.)

This Lua bit lets you happily leave those ugly file extensions off URL's:

  • clean, simple URL's make the web human-friendly
  • actual filename extensions (and application platform) can losslessly change over time
  • script runs as a Lua bytecode machine in lighttpd core; it's fast

Save the following as extensionless_urls.lua next to your lighttpd.conf:


if (not lighty.stat(lighty.env["physical.path"])) then
  file_extensions = { ".html", ".php" }
  for key, file_extension in pairs(file_extensions) do
    if (lighty.stat(lighty.env["physical.path"] .. file_extension)) then
      lighty.env["uri.path"] = lighty.env["uri.path"] .. file_extension
      lighty.env["physical.rel-path"] = lighty.env["uri.path"]
      lighty.env["physical.path"] = 
        lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
      break
    end
  end
end

Note the file_extensions array, which should be composed of the extensions to search for by priority. As shown above, an ".html" file will be found before a ".php" file by the same name. Keep this array short: one entry, if all your files have the same extension.

Adjust lighttpd.conf


# Include the mod_magnet early in the module list.
server.modules = ( "mod_magnet" )

# Call the Lua machine for each physical file request.
# Change this path to match the location of your script.
magnet.attract-physical-path-to = ( 
  "/etc/lighttpd/extensionless_urls.lua" )

Restart lighttpd, and you now have stat-cached, fuzzy URL matching!

To get up-and-running with lighttpd+Lua, see:
mod_magnet on OS X

3 Responses to “Extensionless URL's [with lighttpd]”

mezza commented
2007-07-15 at 10:48 AM
Hi Mars, The link to http://marsorange.com/articles/2006/09/26/mod_magnet-on-os-x seems to be empty in terms of content. Would be very interested in seeing how you built mod_magnet from source on os x. TIA
Mars commented
2007-07-15 at 10:48 AM
Mezza: That article was missing the "extended" content; must have been lost in a recent blog move/upgrade. I've restored it. Thanks for your courteous notice!
Darvas Dorottya commented
2008-04-19 at 04:53 AM

Thank you very much!

Leave a Reply

Markdown is in effect.



Everything is here.