From 5c088b1fd9c82aff92b64d76fc6a11753b356447 Mon Sep 17 00:00:00 2001 From: thinkpadmaster Date: Fri, 21 Jul 2023 22:52:14 -0500 Subject: Redirect alexscerba.com to www.alexscerba.com --- main.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/main.go b/main.go index 6e9c95b..fc31f0e 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "log" "net/http" "os" + "strings" ) type application struct { @@ -23,6 +24,17 @@ func (app *application) httpsRedirect(w http.ResponseWriter, req *http.Request) http.StatusMovedPermanently) } +func (app *application) wwwRedirect(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.Host, "www.") { + http.Redirect(w, r, r.URL.Scheme+"://www."+r.Host+r.RequestURI, 302) + return + } + + h.ServeHTTP(w, r) + }) +} + func main() { infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime) errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile) -- cgit v1.2.3