aboutsummaryrefslogtreecommitdiffstats
path: root/lua/redirect_lower_url.lua
blob: 5aae177927c1339690660eb0b4728cfc2b4105b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- /usr/lib/cgit/lua/debug_gpgfrontend.lua
local r = lighty.r
local a = r.req_attr

-- Get the original path
local raw = a["uri.path-raw"] or a["uri.path"] or "/"
local orig = a["request.orig-uri"] or raw

-- Target path
local new_path

-- 1) /GpgFrontend/...  →  /gpgfrontend/...
if orig:match("^/GpgFrontend") then
  new_path = orig:gsub("^/GpgFrontend", "/gpgfrontend")
-- 2) /cgit.cgi/GpgFrontend/... → /gpgfrontend/... (let rewrite add /cgit.cgi once)
elseif orig:match("^/cgit%.cgi/GpgFrontend") then
  new_path = orig:gsub("^/cgit%.cgi/GpgFrontend", "/gpgfrontend")
end

-- If rule matches, convert repo name to lowercase
if new_path then
  -- Capture repo segment: /gpgfrontend/<repo>/...
  new_path = new_path:gsub("^(/gpgfrontend/)([^/]+)",
    function(prefix, repo)
      return prefix .. repo:lower()
    end)

  -- Append query string
  local q = a["uri.query"]
  if q and #q > 0 and not new_path:find("%?") then
    new_path = new_path .. "?" .. q
  end

  r.resp_header["Location"] = new_path
  return 301
end

-- Visible marker
r.resp_header["X-Magnet-Probed"] = "1"
return 0