Files
pages/internal/server/middleware.go
2026-05-19 21:30:42 +09:00

38 lines
527 B
Go

package server
import (
"net/http"
"strings"
)
func (s *Server) normalizeSlashes(next http.Handler) http.Handler {
return http.HandlerFunc(func(
w http.ResponseWriter,
r *http.Request,
) {
if r.URL.Path != "/" &&
strings.HasSuffix(r.URL.Path, "/") {
target := strings.TrimSuffix(
r.URL.Path,
"/",
)
if r.URL.RawQuery != "" {
target += "?" + r.URL.RawQuery
}
http.Redirect(
w,
r,
target,
http.StatusMovedPermanently,
)
return
}
next.ServeHTTP(w, r)
})
}