From 756f6b324cf4c40c044e5208d5ca05a5771de9d9 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Thu, 23 Sep 2021 18:24:45 +0200 Subject: [PATCH] Old HTTPS redirect again --- http.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index b5f91ad..3643f48 100644 --- a/http.go +++ b/http.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "log" + "net" "net/http" "strconv" "time" @@ -123,9 +124,13 @@ func shutdownServer(s *http.Server, name string) func() { } } -func (a *goBlog) redirectToHttps(w http.ResponseWriter, r *http.Request) { +func (*goBlog) redirectToHttps(w http.ResponseWriter, r *http.Request) { + requestHost, _, err := net.SplitHostPort(r.Host) + if err != nil { + requestHost = r.Host + } w.Header().Set("Connection", "close") - http.Redirect(w, r, fmt.Sprintf("https://%s%s", a.cfg.Server.publicHostname, r.URL.RequestURI()), http.StatusMovedPermanently) + http.Redirect(w, r, fmt.Sprintf("https://%s%s", requestHost, r.URL.RequestURI()), http.StatusMovedPermanently) } const (