kataras/iris · error

redirect match: status code digits: %s [%d:%c]

Error message

redirect match: status code digits: %s [%d:%c]

What it means

parseRedirectMatchLine validates redirect rewrite rules of the form 'CODE PATTERN TARGET'. Every character of the status-code field must be a digit; when a non-digit is found at index i, the parser rejects the whole line with this error, naming the bad character and its position. This prevents a malformed code like '30x' from silently becoming code 30 or 0.

Source

Thrown at middleware/rewrite/rewrite.go:291

		if match := r.pattern.ReplaceAllString(src, r.target); match != "" {
			return match, true
		}
	}

	return "", false
}

func parseRedirectMatchLine(s string) (*redirectMatch, error) {
	parts := strings.Split(strings.TrimSpace(s), " ")
	if len(parts) != 3 {
		return nil, fmt.Errorf("redirect match: invalid line: %s", s)
	}

	codeStr, pattern, target := parts[0], parts[1], parts[2]

	for i, ch := range codeStr {
		if !isDigit(ch) {
			return nil, fmt.Errorf("redirect match: status code digits: %s [%d:%c]", codeStr, i, ch)
		}
	}

	code, err := strconv.Atoi(codeStr)
	if err != nil {
		// this should not happen, we check abt digit
		// and correctly position the error too but handle it.
		return nil, fmt.Errorf("redirect match: status code digits: %s: %v", codeStr, err)
	}

	regex := regexp.MustCompile(pattern)
	if regex.MatchString(target) {
		return nil, fmt.Errorf("redirect match: loop detected: pattern: %s vs target: %s", pattern, target)
	}

	v := &redirectMatch{
		code:              code,
		pattern:           regex,

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Make the first field of the redirect line a pure-digit status code (e.g. 301, 302, 307, 308).
  2. Check that the line has exactly three whitespace-separated fields: code, pattern, target — a missing field shifts values left.
  3. If porting from Apache/nginx configs, rewrite the directive to this library's 'CODE PATTERN TARGET' format.
  4. Validate the code string with a regexp like ^\d{3}$ before calling New in tests or tooling.

Example fix

// before
rewrite.New([]rewrite.RedirectMatchLine{"30x /old(.*) /new/$1"})
// after
rewrite.New([]rewrite.RedirectMatchLine{"301 /old(.*) /new/$1"})
Defensive patterns

Strategy: validation

Validate before calling

var codeRe = regexp.MustCompile(`^\d+$`)
if !codeRe.MatchString(strings.Fields(line)[0]) {
    return fmt.Errorf("invalid redirect line %q: code must be digits", line)
}

Try / catch

if err := rewrite.New(lines); err != nil {
    var perr *parseError
    if errors.As(err, &perr) { /* fix config line */ }
    log.Fatalf("redirect config invalid: %v", err)
}

Prevention

When it happens

Trigger: Registering a Rewrite redirect rule via rewrite.New (or RedirectMatch directive) where the first whitespace-separated field is not purely numeric, e.g. '30x /old /new' or 'redirect /old /new' (missing the code).

Common situations: Typo in a redirect config line, copying an Apache-style RedirectMatch line without adjusting the syntax, or a rule line where the code field was accidentally omitted so pattern/target shift left.

Related errors


AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30). Data as JSON: /api/errors/b02140122f43c24f. Report an issue: GitHub.