about summary refs log tree commit diff stats
path: root/internal/server
diff options
context:
space:
mode:
authorAlan Pearce2024-07-11 14:01:51 +0200
committerAlan Pearce2024-07-11 14:01:51 +0200
commite26a0e83ddb25c40379280f191ff24fb9cc5260f (patch)
treed4a89537793f795ce1b9d45826dac015403883f2 /internal/server
parent4021621241627475a2a392125a09145c085b5e82 (diff)
downloadwebsite-e26a0e83ddb25c40379280f191ff24fb9cc5260f.tar.lz
website-e26a0e83ddb25c40379280f191ff24fb9cc5260f.tar.zst
website-e26a0e83ddb25c40379280f191ff24fb9cc5260f.zip
fix wildcard-based redirects
Diffstat (limited to 'internal/server')
-rw-r--r--internal/server/server.go13
1 files changed, 4 insertions, 9 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index 6ef715b..0bbfba4 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -188,7 +188,7 @@ func New(runtimeConfig *Config, log *log.Logger) (*Server, error) {
 		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 @@ func New(runtimeConfig *Config, log *log.Logger) (*Server, error) {
 				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 {