Before I start, I’d like to make it clear that 95% of this code came from Evan Fosmark, who also had the original idea. I’ve just mashed together some different bits of code and customised it to meet my needs!
When I migrated my website from my old hosting account over to my Virtual Private Server yesterday, I soon realised I had overlooked something that, if left unchanged, would end up annoying me quire a bit – directory listings.
My VPS only has 128MB guaranteed RAM, and that means it’s running lighttpd instead of the altogether meatier Apache. This doesn’t bother me in the slightest – if anything, I prefer lighttpd anyway. I’ve learned how to use it over the past few months and I find it fast, easily-configured, and definitely ‘light’! One of the sacrifices you make for this lightness, however, is that the directory listings aren’t anywhere near as fancy as Apache’s. There are no filetype icons, and no option for sorting the file list by date/size/type.
This led me to look for another way of generating directory listings for my stuff subdomain, which probably gets more use than the main site because it’s where I plonk all the random things that I need to share, but don’t deserve their own page on the site. Finding Evan Fosmark’s Enhanced Lighttpd directory generator saved me from having to use my meagre PHP knowledge to write my own script, but it only worked for the root folder that the index.php was placed in. I wanted it to work, or at least appear to work, for the subfolders too! This is where I remembered mod_rewrite:
- Stick my modified index.php into your root folder and remove the .txt extension
- Add the following to lighttpd.conf (or make your own config file, I saved mine in with the rest of my subdomain configs):
$HTTP["host"] =~ "stuff\.jontysewell\.net" {url.rewrite = ( "^/(?!(.+\.\w*)).*$" => "/index.php?q=$0" )
}
Obviously, replace the host line as required. This regex will make sure that any directories are sent to the generator to have nice indexes made, while files aren’t redirected and are downloaded normally.
- Get the CSS file I use from here, and edit the $css_file variable at the top of index.php to reference wherever you put it.
- To get fancy pants filetype icons like I have, download whatever icon set you like (I’m using Tango icons) and save the relevant files into an .icons folder in the same folder as your CSS. (thanks to Jace for this bit of script)
And that’s about it, I think! If you found that hard to follow, bear in mind it’s the first time I’ve written a guide for this sort of thing, and feel free to ask for help if you need it. I can’t guarantee I’ll reply within any reasonable timeframe, but it’s worth trying!
If I ever get some more time I’d like to modify the script further so that I can put one copy of it in a central place, and use lighttpd rules to give fancy listings to any number of subdomains.
You could also use this instead of your rewrite-magic:
index-file.names = ( “/dir-generator.php” )