gohugoio/hugo · error
failed to compile Redirect regexp %q: %w
Error message
failed to compile Redirect regexp %q: %w
What it means
Raised by Server.CompileConfig when the `fromRe` field of a `[[server.redirects]]` entry fails regexp.Compile. `fromRe` is a Go RE2 regular expression matched against request paths (supporting capture-group substitution in `to`), and an invalid pattern aborts config loading.
Source
Thrown at config/commonConfig.go:277
}
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)
}
return nil
}
func (s *Server) MatchHeaders(pattern string) []types.KeyValueStr {
if s.compiledHeaders == nil {View on GitHub ↗ (pinned to 8a468df065)
Solutions
- Fix the regexp quoted in the error to valid Go RE2 syntax; remove lookarounds and backreferences.
- In TOML, use single-quoted literal strings (`fromRe = '^/post/(\d+)$'`) to avoid backslash-escaping mistakes.
- If you only need wildcard matching, use the `from` glob field instead of `fromRe`.
Example fix
# before (hugo.toml) [[server.redirects]] fromRe = "^/post/(\\d+" to = "/blog/$1" # after [[server.redirects]] fromRe = '^/post/(\d+)$' to = "/blog/$1"
Defensive patterns
Strategy: validation
Validate before calling
if r.FromRe != "" {
if _, err := regexp.Compile(r.FromRe); err != nil {
return fmt.Errorf("redirect 'fromRe' %q invalid: %w", r.FromRe, err)
}
} Try / catch
if err := cfg.Server.CompileConfig(logger); err != nil {
// "failed to compile Redirect regexp" — validate the fromRe with Go's RE2 syntax
return err
} Prevention
- `fromRe` uses Go RE2: no backreferences or lookaround — patterns ported from PCRE/Nginx often fail here
- Prefer raw TOML strings ('...') so backslashes aren't double-escaped
- Test the pattern with `regexp.MustCompile` in a Go playground or regex101 set to Golang
When it happens
Trigger: A `[[server.redirects]]` entry with `fromRe` containing invalid RE2 syntax — e.g. `fromRe = "^/blog/(\\d+"` (unclosed group), PCRE lookarounds like `(?<!x)` / `(?=x)`, or backreferences `\\1`, none of which Go's regexp package supports.
Common situations: Porting nginx/Apache rewrite regexps that use lookahead or backreferences; forgetting to double-escape backslashes in TOML basic strings; putting glob syntax like `**` in `fromRe` where it means something different in a regexp.
Related errors
- redirects must have either From or FromRe set
- failed to compile Redirect glob %q: %w
- failed to compile Redirect header glob %q: %w
- failed to compile cache buster %q: %w
- failed to compile Headers glob %q: %w
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/12f45b0e3a35dec2.json.
Report an issue: GitHub ↗.