Skip to content

Trailing Slash

All core middleware lives in the middleware package:

import "github.com/labstack/echo/v5/middleware"

Add trailing slash middleware adds a trailing slash to the request URI.

e := echo.New()
e.Pre(middleware.AddTrailingSlash())

Remove trailing slash middleware removes a trailing slash from the request URI.

e := echo.New()
e.Pre(middleware.RemoveTrailingSlash())
e := echo.New()
e.Use(middleware.AddTrailingSlashWithConfig(middleware.AddTrailingSlashConfig{
RedirectCode: http.StatusMovedPermanently,
}))

The example above adds a trailing slash to the request URI and redirects with 301 - StatusMovedPermanently.

type AddTrailingSlashConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
// Status code to be used when redirecting the request.
// Optional, but when provided the request is redirected using this code.
// Valid status codes: [300...308]
RedirectCode int
}
type RemoveTrailingSlashConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
// Status code to be used when redirecting the request.
// Optional, but when provided the request is redirected using this code.
RedirectCode int
}