Skip to content

Commit

Permalink
Merge pull request #5 from martinjirku/redirect
Browse files Browse the repository at this point in the history
redirect from www to the root
  • Loading branch information
martinjirku authored Nov 20, 2024
2 parents b4fc9b3 + 5e02bcf commit dea8c8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func prepareServices(log *slog.Logger, fs fs.FS) (*services.CSS, *services.Spons
}

func prepareMiddleware(router *mux.Router, log *slog.Logger, storeService sessions.Store) {
router.Use(middleware.RedirectFromWWWSubdomain(log))
router.Use(middleware.Recover(middlewareLog(log, "recover")))
router.Use(middleware.RequestID(middlewareLog(log, "requestID")))
router.Use(middleware.Logger(middlewareLog(log, "logger")))
Expand Down
25 changes: 25 additions & 0 deletions pkg/middleware/redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package middleware

import (
"log/slog"
"net/http"
)

func RedirectFromWWWSubdomain(logger *slog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host == "www.mcmamina.sk" {
if r.URL.RawQuery != "" {
target := "https://mcmamina.sk"
// target := fmt.Sprintf("https://mcmamina.sk%s", r.URL.Path)
// target += "?" + r.URL.RawQuery
logger.Info("redirecting", slog.String("host", r.Host), slog.String("target", target))
http.Redirect(w, r, target, http.StatusMovedPermanently)
return
}

}
next.ServeHTTP(w, r)
})
}
}

0 comments on commit dea8c8b

Please sign in to comment.