labstack/echo · error

redirectConfig is missing redirect function

Error message

redirectConfig is missing redirect function

What it means

Returned by RedirectConfig.ToMiddleware when the unexported config.redirect function is nil. That function implements the specific redirect decision (HTTPS, WWW, etc.) and is set by the preset constructors like HTTPSRedirectWithConfig; a bare RedirectConfig{} has no redirect logic. Because the field is unexported, you cannot set it yourself — you must use a preset or one of the WithConfig helpers.

Source

Thrown at middleware/redirect.go:127

	return NonWWWRedirectWithConfig(RedirectNonWWWConfig)
}

// NonWWWRedirectWithConfig returns a Non-WWW redirect middleware with config or panics on invalid configuration.
func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
	config.redirect = redirectNonWWW
	return toMiddlewareOrPanic(config)
}

// ToMiddleware converts RedirectConfig to middleware or returns an error for invalid configuration
func (config RedirectConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
	if config.Skipper == nil {
		config.Skipper = DefaultSkipper
	}
	if config.Code == 0 {
		config.Code = http.StatusMovedPermanently
	}
	if config.redirect == nil {
		return nil, errors.New("redirectConfig is missing redirect function")
	}

	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c *echo.Context) error {
			if config.Skipper(c) {
				return next(c)
			}

			req, scheme := c.Request(), c.Scheme()
			host := req.Host
			if ok, url := config.redirect(scheme, host, req.RequestURI); ok {
				return c.Redirect(config.Code, url)
			}

			return next(c)
		}
	}, nil
}

View on GitHub (pinned to 05489dc173)

Solutions

  1. Start from a preset, e.g. HTTPSRedirectWithConfig(RedirectHTTPSConfig) which sets redirect=redirectHTTPS.
  2. To customize the status code, copy a preset and change only Code: cfg := RedirectHTTPSConfig; cfg.Code = http.StatusMovedPermanently; HTTPSRedirectWithConfig(cfg).
  3. Pick the constructor matching your goal: HTTPSRedirect, HTTPSWWWRedirect, HTTPSNonWWWRedirect, WWWRedirect, NonWWWRedirect (each has a WithConfig variant).
  4. Do not hand-construct RedirectConfig{} expecting redirect logic — the field cannot be set externally.

Example fix

// before (will panic: redirect is nil)
m := toMiddlewareOrPanic(middleware.RedirectConfig{Code: 301})
// after
cfg := middleware.RedirectHTTPSConfig
cfg.Code = http.StatusMovedPermanently
m := middleware.HTTPSRedirectWithConfig(cfg)
Defensive patterns

Strategy: validation

Validate before calling

// Always start from a preset; never hand-construct RedirectConfig{}.
func httpsRedirectMiddleware(code int) (echo.MiddlewareFunc, error) {
    if code != 0 && (code < 300 || code > 308) {
        return nil, errors.New("redirect code must be a 3xx")
    }
    cfg := middleware.RedirectHTTPSConfig
    cfg.Code = code
    return cfg.ToMiddleware()
}

Prevention

When it happens

Trigger: Constructing RedirectConfig{} and passing it to toMiddlewareOrPanic or its ToMiddleware directly, instead of going through HTTPSRedirectWithConfig / WWWRedirectWithConfig / etc. Also reachable if you copy a preset config and zero out its redirect logic via reflection or by reassigning the whole struct.

Common situations: Developer wants a custom Code and builds RedirectConfig{Code: 301} manually instead of starting from RedirectHTTPSConfig; or is unaware that redirect logic is bound by calling the specific WithConfig constructor.

Related errors


AI-assisted analysis of labstack/echo@05489dc173 (2026-08-04). Data as JSON: /data/errors/c8a1b230d4cc559c.json. Report an issue: GitHub.