1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-05 22:53:57 +02:00
echo/website/content/middleware/redirect.md
Vishal Rana 73110004ef doc: updated, recipe: auto tls
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-11-28 20:34:29 -08:00

1.9 KiB

+++ title = "Redirect Middleware" description = "Redirect middleware for Echo" [menu.main] name = "Redirect" parent = "middleware" weight = 5 +++

HTTPS Redirect

HTTPS redirect middleware redirects http requests to https. For example, http://labstack.com will be redirected to https://labstack.com.

Usage

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

HTTPS WWW Redirect

HTTPS WWW redirect redirects http requests to www https. For example, http://labstack.com will be redirected to https://www.labstack.com.

Usage

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

HTTPS NonWWW Redirect

HTTPS NonWWW redirect redirects http requests to https non www. For example, http://www.labstack.com will be redirect to https://labstack.com.

Usage

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

WWW Redirect

WWW redirect redirects non www requests to www.

For example, http://labstack.com will be redirected to http://www.labstack.com.

Usage

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

NonWWW Redirect

NonWWW redirect redirects www requests to non www. For example, http://www.labstack.com will be redirected to http://labstack.com.

Usage

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

Custom Configuration

Usage

e := echo.New()
e.Use(middleware.HTTPSRedirectWithConfig(middleware.RedirectConfig{
  Code: http.StatusTemporaryRedirect,
}))

Example above will redirect the request HTTP to HTTPS with status code 307 - StatusTemporaryRedirect.

Configuration

RedirectConfig struct {
  // Skipper defines a function to skip middleware.
  Skipper Skipper

  // Status code to be used when redirecting the request.
  // Optional. Default value http.StatusMovedPermanently.
  Code int `json:"code"`
}

Default Configuration

DefaultRedirectConfig = RedirectConfig{
  Skipper: defaultSkipper,
  Code:    http.StatusMovedPermanently,
}