Skip to content

Trailing Slash

All core middleware lives in the middleware package:

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

When configured to redirect, the trailing slash middlewares percent-encode control characters in the path before constructing the Location header.

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.

AddTrailingSlashConfig · github.com/labstack/echo/[email protected]

Fields from package source
FieldTypeDescriptionSource
SkipperSkipperSkipper defines a function to skip middleware.L17
RedirectCodeintStatus code to be used when redirecting the request. Optional, but when provided the request is redirected using this code. Valid status codes: [300...308]L22
Functions from package source
TypeSource
func AddTrailingSlash() echo.MiddlewareFuncL29
func AddTrailingSlashWithConfig(config AddTrailingSlashConfig) echo.MiddlewareFuncL34

RemoveTrailingSlashConfig · github.com/labstack/echo/[email protected]

Fields from package source
FieldTypeDescriptionSource
SkipperSkipperSkipper defines a function to skip middleware.L82
RedirectCodeintStatus code to be used when redirecting the request. Optional, but when provided the request is redirected using this code.L86
Functions from package source
TypeSource
func RemoveTrailingSlash() echo.MiddlewareFuncL93
func RemoveTrailingSlashWithConfig(config RemoveTrailingSlashConfig) echo.MiddlewareFuncL98