gohugoio/hugo · error

failed to compile Redirect glob

Error message

failed to compile Redirect glob %q: %w

What it means

Emitted by Server.CompileConfig when a redirect's `from` glob fails to compile via gobwas/glob. Distinct from 277 (regexp) and 278 (header glob); this is specifically the redirect's primary glob matcher.

Solutions

  1. Validate `from` as a glob (not regexp).
  2. Balance braces/brackets; simplify to `/path/*` to confirm.

Example fix

# before
[[server.redirects]]
from = "/old{a,b"
to = "/new"

# after
[[server.redirects]]
from = "/old{a,b}"
to = "/new"
Defensive patterns

Strategy: validation

Validate before calling

python3 - <<'PY'
import tomllib
cfg=tomllib.loads(open('hugo.toml').read())
for r in cfg.get('server',{}).get('redirects',[]):
    f=r.get('from','')
    if f:
        assert f.count('{')==f.count('}') and f.count('[')==f.count(']'), f"unbalanced glob {f!r}"
print("redirect globs ok")
PY

Prevention

When it happens

Trigger: A `[[server.redirects]]` with `from` containing invalid glob syntax such as an unclosed brace or bracket.

Common situations: Using regex metacharacters in a glob field; malformed brace expansion like `/old{a,b`.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/2039b9a8697f7501. Report an issue: GitHub.

Appendix: source

Thrown at config/commonConfig.go:270

	}
	for _, h := range s.Headers {
		g, err := glob.Compile(h.For)
		if err != nil {
			return fmt.Errorf("failed to compile Headers glob %q: %w", h.For, err)
		}
		s.compiledHeaders = append(s.compiledHeaders, g)
	}
	for _, r := range s.Redirects {
		if r.From == "" && r.FromRe == "" {
			return fmt.Errorf("redirects must have either From or FromRe set")
		}
		rd := redirect{
			headers: make(map[string]glob.Glob),
		}
		if r.From != "" {
			g, err := glob.Compile(r.From)
			if err != nil {
				return fmt.Errorf("failed to compile Redirect glob %q: %w", r.From, err)
			}
			rd.from = g
		}
		if r.FromRe != "" {
			re, err := regexp.Compile(r.FromRe)
			if err != nil {
				return fmt.Errorf("failed to compile Redirect regexp %q: %w", r.FromRe, err)
			}
			rd.fromRe = re
		}
		for k, v := range r.FromHeaders {
			g, err := glob.Compile(v)
			if err != nil {
				return fmt.Errorf("failed to compile Redirect header glob %q: %w", v, err)
			}
			rd.headers[k] = g
		}
		s.compiledRedirects = append(s.compiledRedirects, rd)

View on GitHub (pinned to 52c9bd7908)