aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go12
1 files changed, 12 insertions, 0 deletions
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)