By default, it looks like lighttpd doesn’t resolve html files without the extension. I’m trying to setup quartz – https://quartz.jzhao.xyz/ and that has html files, but links are all without the htmls.
I’m trying to open http://example.com/abcd
, which should open http://example.com/abcd.html
(the html is present in the server) but it’s not resolving the html extension part, and is throwing a 404.
Also, I need this to happen for multiple URLs, and they’re all dynamic, so this won’t work for me: How do I use rewrite on Lighttpd?
So far, I used this config:
url.rewrite-repeat-if-not-file = ( "^(.*)/index.html$" => "$1.html", "" => "${url.path}/index.html" )
So, if there’s no matching file, it will add /index.html. If it has a /index.html (might have been added as part of the 2nd rule), then take up to before /index.html and add.html.
This doesn’t solve my problem either, because it will not throw 404 in case a file indeed doesn’t exist. It will instead find an infinite loop in the rules and reset connection.
This also doesn’t work for me
url.rewrite-repeat-if-not-file = ( "^(.*)/index.html$" => "$1.html", "(.*)/$" => "${url.path}/index.html" )
So, if there’s no matching file and the matcher ends with /
, which it should if there’s no extension being passed from the client, it will add /index.html. If it has a /index.html (might have been added as part of the 2nd rule), then take upto before /index.html and add.html. This is working by throwing 404 for actually non-existent files. The .html is being added when passed without the extension but the file is there. And the index.html is being added when no extension is passed (browser should request for the url followed by a /
, assuming folder).
Turns out my assumptions were incorrect. The server isn’t getting a /
and thus the 2nd rule is not getting hit. Either way, this brings me back to square 1 where the .html is not being added.
Yes, many of your assumptions are incorrect, but kudos to you for testing those assumptions yourself to find that out.
A simplistic solution might be to rewrite any url without a ‘.ext’ to ‘.html’ using a negative lookahead regex pattern
url.rewrite-if-not-file = ( "(?!.[^/]*)$" => "${url.path}.html" )
but that may also rewrite other paths, e.g. if you serve directory listings.
A thorough solution is to use mod_magnet with a short custom lua script to test if the url with .html appended resolves to a file, and if so rewrite it, and if not, leave it alone.
Another solution is to a custom script as a target for mod_indexfile, e.g. index-file.names = ( "index.py" )
to do something similar in Python to what you can do with mod_magnet, but I personally prefer mod_magnet, which is much faster since it runs inside lighttpd.
Edit: adding mod_magnet example (Note: you need to replace /path/to/
with actual path)
lighttpd.conf
server.modules += ("mod_magnet")
magnet.attract-physical-path-to = ("/path/to/pretty-url-to-html.lua")
/path/to/pretty-url-to-html.lua
-- Check if path has an extension or exists in filesystem,
-- else check filesystem for path with ".html" appended.
local path = lighty.r.req_attr["physical.path"]
if (not string.match(path, "%.[^/]*$") and not lighty.c.stat(path)) then
local st = lighty.c.stat(path .. ".html")
if (st and st.is_file) then
return st["http-response-send-file"]
end
end