Serving static files without file extensions using Lighttpd and Lua

URLs shouldn't really contain file extensions (like .html, .png) since they are supposed to identify a resource and not a particular representation/format thereof. The format is indicated by the Content-Type header sent in the response. Modern CMSs do this already (for example, the URL of this page doesn't include .html).

Doing the same for static files (i.e. files served directly by the webserver) isn't straightforward because most webservers use the file extension to determine the MIME type to send in the Content-Type header. This means that simply removing the file extension from the filename (or even creating a symlink without a file extension) will cause the webserver to send the wrong Content-Type header.

I decided to try find a solution to this for my webserver of choice, Lighttpd. Lighttpd has a module which embeds a [Lua][] interpreter and allows you to write scripts which modify (or even handle) requests. So I wrote a [script][] which searches the directory for files with the same name as requested but with an extension. This means that any file can be accessed with the file extension removed from the URL while still having the correct Content-Type.

The script currently chooses the first matching file, which means that having multiple files with the same name but different extensions doesn't do anything useful. The proper method however is to actually do [content negotiation][], which chooses the format based on the preferences indicated by the HTTP client in the Accept header.

To use this script, download it and save it somewhere (I use /etc/lighttpd/). Enable mod_magnet, and add the following line to the site definition.

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

Trackback URL for this post:

http://michael.gorven.za.net/trackback/1486
AttachmentSize
extension.lua558 bytes

I'm more than happy to

I'm more than happy to uncover this website. I need to to
thank you for your time due to this wonderful read!!

I definitely loved every little bit of it and i also have you saved to fav to check out new things on your blog.

I have now written a script

I have now written a script which implements proper content negotiation.

Whats up this is kinda of off

Whats up this is kinda of off topic but I was wondering if blogs use
WYSIWYG editors or if you have to manually code with HTML.
I'm starting a blog soon but have no coding know-how so I wanted to get advice from
someone with experience. Any help would be enormously appreciated!

I'm not sure exactly why but

I'm not sure exactly why but this website is loading incredibly slow for
me. Is anyone else having this problem or is it a issue on my end?
I'll check back later and see if the problem still exists.