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/v5@5196b9b

Fields from package source
FieldTypeDescriptionSource
Skipper Skipper Skipper defines a function to skip middleware. L17
RedirectCode int 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] L22
Functions from package source
TypeSource
func AddTrailingSlash() echo.MiddlewareFunc L29
func AddTrailingSlashWithConfig(config AddTrailingSlashConfig) echo.MiddlewareFunc L34

RemoveTrailingSlashConfig · github.com/labstack/echo/v5@5196b9b

Fields from package source
FieldTypeDescriptionSource
Skipper Skipper Skipper defines a function to skip middleware. L82
RedirectCode int Status 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.MiddlewareFunc L93
func RemoveTrailingSlashWithConfig(config RemoveTrailingSlashConfig) echo.MiddlewareFunc L98