diff --git a/plugins/web/internal/httpx/listen.go b/plugins/web/internal/httpx/listen.go
index d348f40..a9fba3f 100644
--- a/plugins/web/internal/httpx/listen.go
+++ b/plugins/web/internal/httpx/listen.go
@@ -247,6 +247,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
handler := withMiddleware(mux, mw)
handler = withRBAC(handler, authCfg.gates, authCfg.loginPath)
handler = withTenant(handler, st.tenant)
+ handler = withStripTrailingSlash(handler)
return handler, nil
}
@@ -641,6 +642,21 @@ func withTenant(next http.Handler, cfg tenant.Config) http.Handler {
})
}
+// withStripTrailingSlash rewrites request URLs so that paths like /post/foo/
+// are routed as /post/foo. The root path "/" is left unchanged.
+func withStripTrailingSlash(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ path := r.URL.Path
+ if len(path) > 1 && path[len(path)-1] == '/' {
+ r.URL.Path = strings.TrimRight(path, "/")
+ if r.URL.Path == "" {
+ r.URL.Path = "/"
+ }
+ }
+ next.ServeHTTP(w, r)
+ })
+}
+
func withMiddleware(next http.Handler, mw middleware.Config) http.Handler {
h := next
if mw.BodyLimit != nil && *mw.BodyLimit > 0 {
diff --git a/plugins/web/internal/httpx/listen_test.go b/plugins/web/internal/httpx/listen_test.go
index a544c58..a41c6bf 100644
--- a/plugins/web/internal/httpx/listen_test.go
+++ b/plugins/web/internal/httpx/listen_test.go
@@ -152,3 +152,65 @@ func TestNewHandlerIconsRedirectSitemap404(t *testing.T) {
t.Fatalf("home missing icon link: %s", rec.Body.String())
}
}
+
+func TestTrailingSlashNormalization(t *testing.T) {
+ page := map[string]any{"title": "Home", "intro": "
home
"}
+ a := app.New(map[string]any{"page": page})
+ about := map[string]any{"title": "About", "intro": "About
", "_route": "/about"}
+ var err error
+ a, err = app.Route(a, "/about", about)
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Dynamic route: /post/{slug}
+ post := map[string]any{"title": "Post", "intro": "Post
", "_route": "/post/{slug}"}
+ a, err = app.Route(a, "/post/{slug}", post)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ h, err := httpx.NewHandler(a, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Static route: /about should work with and without trailing slash.
+ rec := httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about", nil))
+ if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
+ t.Fatalf("about (no slash) code=%d body=%q", rec.Code, rec.Body.String())
+ }
+
+ rec = httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about/", nil))
+ if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
+ t.Fatalf("about (trailing slash) code=%d body=%q", rec.Code, rec.Body.String())
+ }
+
+ // Dynamic route: /post/hello should work with and without trailing slash.
+ rec = httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/post/hello", nil))
+ if rec.Code != 200 {
+ t.Fatalf("post/hello (no slash) code=%d body=%q", rec.Code, rec.Body.String())
+ }
+
+ rec = httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/post/hello/", nil))
+ if rec.Code != 200 {
+ t.Fatalf("post/hello (trailing slash) code=%d body=%q", rec.Code, rec.Body.String())
+ }
+
+ // Root path "/" should still work.
+ rec = httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
+ if rec.Code != 200 || !strings.Contains(rec.Body.String(), "Home") {
+ t.Fatalf("root code=%d body=%q", rec.Code, rec.Body.String())
+ }
+
+ // Query string should be preserved after trailing slash strip.
+ rec = httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about/?foo=bar", nil))
+ if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
+ t.Fatalf("about (trailing slash + query) code=%d body=%q", rec.Code, rec.Body.String())
+ }
+}