tagged lua

by mars on 2007-05-17 3 Comments

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

by mars on 2006-09-26 0 Comments

Update 18 May 2007: While this article may still be useful, the original purpose of this prose was to explain how to implement extensionless URL's with lighttpd, which is now updated & improved, no LFS necessary.

URL rewriting is a serious tool for the web developer; a little known world of regular expressions, domain specific languages, HTTP protocol manipulation, and web server mechanics.

I've been using an Apache mod_rewrite sequence that enables extension-less URL's to HTML files at almost every site I've produced over the past five years. This (and many other) Apache-specific mod_rewrite functionality has not been so easy to move to lighttpd, whose mod_rewrite really just does simple regular expression search & replace on URL's.

Until now, as of lighttpd 1.4.12, we finally have mod_magnet, a Lua-language execution environment running in lighttpd's core for lightning-fast, complex URL manipulations. With the LuaFileSystem [lfs] module, it's possible to check for existence of files & directories inside a Lua script.

LuaFileSystem for on OS X

Lua is a bit difficult to get working on OS X, and LuaFileSystem even more so. Follow along to lighttpd URL-rewriting heaven.

Read the rest of this entry

by mars on 2006-09-25 3 Comments

Are you attracted by lighttpd 1.4.12's new mod_magnet?

mod_magnet is a solution to the long-standing quandary of complex URL rewriting without Apache's mod_rewrite. Don't be confused by lighttpd's mod_rewrite; that module should really be called mod_url_find_and_replace.

The gotcha: mod_magnet requires Lua, an embedded programming language created in Brazil. This is the same language that lighttpd's mod_cml uses for Cache Meta Language.

I tried installing Lua via DarwinPorts, but it didn't work. Lighttpd would just complain if I tried enabling mod_magnet:


%  /usr/local/sbin/lighttpd -f ~/etc/lighttpd.conf -D
2006-09-24 22:18:05: (plugin.c.213) mod_magnet plugin init failed
2006-09-24 22:18:05: (server.c.577) loading plugins finally failed

After much tinkering, here's how I got mod_magnet working.

Read the rest of this entry


Everything is here.