trailing-slash

Trailing Slash

原文:https://echo.labstack.com/docs/middleware/trailing-slash

Add Trailing Slash

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

Usage

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

Remove Trailing Slash

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

Usage

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

Custom Configuration

Usage

1
2
3
4
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 301 - StatusMovedPermanently.

xxxxxxxxxx5 1DefaultTimeoutConfig = TimeoutConfig{2    Skipper:      DefaultSkipper,3    Timeout:      0,4    ErrorHandler: nil,5}go

1
2
3
4
5
6
7
8
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

1
2
3
DefaultTrailingSlashConfig = TrailingSlashConfig{
  Skipper: DefaultSkipper,
}
最后修改 October 10, 2024: 更新 (a4b8f85)