#!/usr/bin/env lua -- Perl-like split/join functions by http://lua-users.org/wiki/PeterPrade -- Concat the contents of the parameter list, -- separated by the string delimiter (just like in perl) -- example: strjoin(", ", {"Anna", "Bob", "Charlie", "Dolores"}) function strjoin(delimiter, list) local len = #list if len == 0 then return "" end local string = list[1] for i = 2, len do string = string .. delimiter .. list[i] end return string end -- Split text into a list consisting of the strings in text, -- separated by strings matching delimiter (which may be a pattern). -- example: strsplit(",%s*", "Anna, Bob, Charlie,Dolores") function strsplit(delimiter, text) local list = {} local pos = 1 if string.find("", delimiter, 1) then -- this would result in endless loops error("delimiter matches empty string!") end while 1 do local first, last = string.find(text, delimiter, pos) if first then -- found? table.insert(list, string.sub(text, pos, first-1)) pos = last+1 else table.insert(list, string.sub(text, pos)) break end end return list end function lowercase_textpattern_categories(request_uri) local uri_parts = strsplit("/", request_uri) if #uri_parts >= 3 then uri_parts[3] = uri_parts[3]:lower() end return strjoin("/", uri_parts) end lighty.env["request.orig-uri"] = lighty.env["request.uri"] lighty.env["request.uri"] = lowercase_textpattern_categories(lighty.env["request.orig-uri"]) if lighty.env["request.uri"] ~= lighty.env["request.orig-uri"] then print("SCHMONZ: redirecting " .. lighty.env["request.orig-uri"] .. " to " .. lighty.env["request.uri"]) lighty.header["Location"] = lighty.env["request.uri"] return 302 else print("SCHMONZ: unneeded Lua invocation, no redirect needed for " .. lighty.env["request.orig-uri"]) end