From cf6247040538ac64476de134b6fa41b3f5b6e999 Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Sat, 15 Jun 2024 16:05:43 -0400 Subject: [PATCH] Add support for PermanentRedirects through configuration --- data.go | 1 + main.go | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/data.go b/data.go index f47084a..4ce533f 100644 --- a/data.go +++ b/data.go @@ -44,6 +44,7 @@ type Configuration struct { CMSHeaders bool `json:"cms_headers"` // set CMS headers RedirectURL string `json:"redirect_url"` // redirect auth url for proxy server Verbose int `json:"verbose"` // verbose output + PermanentRedirects []string `json:"permanent_redirects"` // list of permanent redirects Ingress []Ingress `json:"ingress"` // incress section ServerCrt string `json:"server_cert"` // server certificate ServerKey string `json:"server_key"` // server certificate diff --git a/main.go b/main.go index 946a854..a0adb1b 100644 --- a/main.go +++ b/main.go @@ -209,16 +209,17 @@ func srvURL(surl string) string { // helper function to redirect HTTP requests based on configuration ingress rules func redirect(w http.ResponseWriter, r *http.Request) { + // check for permanent redirects first + if !InList(r.URL.Path, Config.PermanentRedirects) { + path := fmt.Sprintf("%s/index.html", r.URL.Path) + path = strings.Replace(path, "//", "/", -1) + http.Redirect(w, r, path, http.StatusMovedPermanently) + return + } // get redirect rule map and rules (in reverse order) // here the reverse order will provide /path/rse /path/aaa followed by /path, etc. // such that we can match the /path as last reserve rmap, rules := RedirectRules(Config.Ingress) - // special CMS cases - if r.URL.Path == "/wmstats" { - r.URL.Path = "/wmstats/index.html" - } else if r.URL.Path == "/tier0_wmstats" { - r.URL.Path = "/tier0_wmstats/index.html" - } for _, key := range rules { rec := rmap[key] // check that request URL path had ingress path with slash