Redirect
All core middleware lives in the middleware package:
import "github.com/labstack/echo/v5/middleware"HTTPS Redirect
Section titled “HTTPS Redirect”HTTPS redirect middleware redirects HTTP requests to HTTPS. For example,
http://labstack.com is redirected to https://labstack.com.
e := echo.New()e.Pre(middleware.HTTPSRedirect())HTTPS WWW Redirect
Section titled “HTTPS WWW Redirect”HTTPS WWW redirect redirects HTTP requests to www HTTPS. For example,
http://labstack.com is redirected to https://www.labstack.com.
e := echo.New()e.Pre(middleware.HTTPSWWWRedirect())HTTPS NonWWW Redirect
Section titled “HTTPS NonWWW Redirect”HTTPS NonWWW redirect redirects HTTP requests to non-www HTTPS. For example,
http://www.labstack.com is redirected to https://labstack.com.
e := echo.New()e.Pre(middleware.HTTPSNonWWWRedirect())WWW Redirect
Section titled “WWW Redirect”WWW redirect redirects non-www requests to www. For example, http://labstack.com is
redirected to http://www.labstack.com.
e := echo.New()e.Pre(middleware.WWWRedirect())NonWWW Redirect
Section titled “NonWWW Redirect”NonWWW redirect redirects www requests to non-www. For example, http://www.labstack.com is
redirected to http://labstack.com.
e := echo.New()e.Pre(middleware.NonWWWRedirect())Custom configuration
Section titled “Custom configuration”e := echo.New()e.Use(middleware.HTTPSRedirectWithConfig(middleware.RedirectConfig{ Code: http.StatusTemporaryRedirect,}))The example above redirects HTTP requests to HTTPS with status code
307 - StatusTemporaryRedirect.
Configuration
Section titled “Configuration”type 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}Default configuration
Section titled “Default configuration”// Effective defaults applied when fields are left unset.RedirectConfig{ Skipper: DefaultSkipper, Code: http.StatusMovedPermanently,}