aboutsummaryrefslogtreecommitdiffstats
path: root/lua/redirect_lower_url.lua
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2025-11-08 15:48:00 +0000
committersaturneric <[email protected]>2025-11-08 15:48:00 +0000
commit51263be0708a6803f044ac0da16bb22b26d1f8b2 (patch)
treec60ed48eb0e9f93d933cf6d2793ad4b176002934 /lua/redirect_lower_url.lua
parentfeat(server): add URL redirect for uppercase paths (diff)
downloadcgit-51263be0708a6803f044ac0da16bb22b26d1f8b2.tar.gz
cgit-51263be0708a6803f044ac0da16bb22b26d1f8b2.zip
fix(redirect): lowercase repo names in redirect URLs
* Ensures repository names in `/gpgfrontend/` redirects are always lowercase for consistency and compatibility. * Handles both direct and rewritten URLs, preserving query strings. * Improves URL normalization and prevents duplicate rewrite segments.
Diffstat (limited to 'lua/redirect_lower_url.lua')
-rw-r--r--lua/redirect_lower_url.lua19
1 files changed, 14 insertions, 5 deletions
diff --git a/lua/redirect_lower_url.lua b/lua/redirect_lower_url.lua
index 74ffc30..5aae177 100644
--- a/lua/redirect_lower_url.lua
+++ b/lua/redirect_lower_url.lua
@@ -2,30 +2,39 @@
local r = lighty.r
local a = r.req_attr
--- Get the "original" path to avoid being affected by rewrites
+-- Get the original path
local raw = a["uri.path-raw"] or a["uri.path"] or "/"
local orig = a["request.orig-uri"] or raw
--- Match two entry points:
--- 1) /GpgFrontend/...
--- 2) /cgit.cgi/GpgFrontend/... (when the user accessed cgit.cgi directly)
+-- 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
- -- Convert /cgit.cgi/GpgFrontend/... to /gpgfrontend/... so rewrite adds /cgit.cgi only once
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 \ No newline at end of file