all repos — homestead @ be1c3810f54a3dcfeb0ff0b044095749403b43d6

Code for my website

fix wildcard-based redirects

Alan Pearce
commit

be1c3810f54a3dcfeb0ff0b044095749403b43d6

parent

c29582cf0f9c3182d8c1d51b6f68c14b75cde0df

1 file changed, 4 insertions(+), 9 deletions(-)

jump to
M internal/server/server.gointernal/server/server.go
@@ -188,7 +188,7 @@ if runtimeConfig.Redirect {
re := regexp.MustCompile( "^(.*)\\." + strings.ReplaceAll(config.WildcardDomain, ".", `\.`) + "$", ) - replace := "${0}." + config.Domains[0] + replace := "${1}." + config.Domains[0] loggingMux.Handle(config.BaseURL.Hostname()+"/", mux) loggingMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if slices.Contains(config.Domains, r.Host) {
@@ -196,14 +196,9 @@ path, _ := website.CanonicalisePath(r.URL.Path)
newURL := config.BaseURL.JoinPath(path) http.Redirect(w, r, newURL.String(), http.StatusMovedPermanently) } else { - matches := re.FindSubmatchIndex([]byte(r.Host)) - if matches == nil { - http.Redirect(w, r, config.BaseURL.String(), http.StatusTemporaryRedirect) - - return - } - newHost := re.ExpandString([]byte{}, replace, r.Host, matches) - http.Redirect(w, r, string(newHost), http.StatusTemporaryRedirect) + url := config.BaseURL + url.Host = re.ReplaceAllString(r.Host, replace) + http.Redirect(w, r, url.String(), http.StatusTemporaryRedirect) } }) } else {