diff options
-rw-r--r-- | main.go | 12 |
1 files changed, 12 insertions, 0 deletions
@@ -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) |