Ok, you could have thought that ”/drupal-5.1” might cause trouble. Sadly it does. The ”-” is a special char for lua patterns. (for more see chapter 5.4.1 of the lua docs)

So i visited my friends at #lua on freenode. After a few minutes of discussion with rici and prec, we got a new drupal.lua. It no longer uses patterns but a simple substring function. And now we happily work with ”/drupal-5.1” aswell.

The details

Why did it fail in first place?

-- prefix without the trailing slash
local prefix = "/drupal-5.1" 

and later we did:

request_uri = string.gsub(lighty.env["uri.path"], prefix .. "/(.+)", "%1")

We pass an unescaped string to the pattern match. Especially the ”-” was causing trouble here. Of course there would have been the option to quote all the special chars properly with ”%”. But thats more work than what is really needed. Rici and prec suggest do add a removePrefix function based on string.sub

function removePrefix(str, prefix)
  return str:sub(1,#prefix+1) == prefix.."/" and str:sub(#prefix+2)
end
-- calculate the relevant part of the request_uri
request_uri = removePrefix(lighty.env["uri.path"], prefix)

Last but not least rici suggest a small optimization to the query string construction. From the old ugly …

-- make sure the request_uri is not empty.
if (string.gmatch(request_uri, ".+")) then
  if lighty.env["uri.query"] then
      lighty.env["uri.query"] = lighty.env["uri.query"] .. "&q=" .. request_uri
  else
      lighty.env["uri.query"] = "q=" .. request_uri
  end
end

... we come to the new and shiny …

local uriquery = lighty.env["uri.query"] or "" 
lighty.env["uri.query"] = uriquery .. (uriquery ~= "" and "&" or "") .. "q=" .. request_uri

You learn something new every day.