1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-14 10:23:00 +02:00
echo/website/content/middleware/trailing-slash.md
Vishal Rana b6547dde66 recipe & website in the main repo
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-10-20 11:30:53 -07:00

1.2 KiB

+++ title = "TrailingSlash Middleware" description = "Trailing slash middleware for Echo" [menu.side] name = "TrailingSlash" parent = "middleware" weight = 5 +++

AddTrailingSlash Middleware

AddTrailingSlash middleware adds a trailing slash to the request URI.

Usage

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

RemoveTrailingSlash Middleware

RemoveTrailingSlash middleware removes a trailing slash from the request URI.

Usage

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

Custom Configuration

Usage

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

Example above will add a trailing slash to the request URI and redirect with 308 - StatusMovedPermanently.

Configuration

TrailingSlashConfig 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 `json:"redirect_code"`
}

Default Configuration

DefaultTrailingSlashConfig = TrailingSlashConfig{
  Skipper: defaultSkipper,
}