Trailing Slash

Add Trailing Slash

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

Usage

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

Remove Trailing Slash

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

Usage

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

Custom Configuration

Usage

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

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

Configuration

  1. TrailingSlashConfig struct {
  2. // Skipper defines a function to skip middleware.
  3. Skipper Skipper
  4. // Status code to be used when redirecting the request.
  5. // Optional, but when provided the request is redirected using this code.
  6. RedirectCode int `json:"redirect_code"`
  7. }

Default Configuration

  1. DefaultTrailingSlashConfig = TrailingSlashConfig{
  2. Skipper: DefaultSkipper,
  3. }