kataras/iris · error

redirect match: invalid line: %s

Error message

redirect match: invalid line: %s

What it means

rewrite's parseRedirectMatchLine expects each redirect-match directive line to contain exactly three space-separated parts: status code, pattern, target. A line that splits into a different number of parts is rejected with this error quoting the whole line. It surfaces when building a RewriteEngine from a file or slice of redirect-match lines.

Source

Thrown at middleware/rewrite/rewrite.go:284

	isRelativePattern bool
	noRedirect        bool
}

func (r *redirectMatch) matchAndReplace(src string) (string, bool) {
	if r.pattern.MatchString(src) {
		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)

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Make each line exactly: <statusCode> <pattern> <target>, e.g. `301 /old /new`.
  2. Remove extra tokens (flags, comments on the same line, brackets) or percent-encode spaces inside URLs.
  3. Use rewrite.TestRedirectMatch/parse on your rules file locally to find the offending line number before deploying.

Example fix

// before
redirect match 301 /old-path /new-path [R=301,L]

// after
redirect match 301 /old-path /new-path
Defensive patterns

Strategy: validation

Validate before calling

parts := strings.Fields(line); if len(parts) != 3 { return fmt.Errorf("bad redirect line: %s", line) }

Type guard

func isValidRedirectMatchLine(s string) bool { return len(strings.Fields(strings.TrimSpace(s))) == 3 }

Try / catch

_, err := rewrite.New(rules); if err != nil && strings.Contains(err.Error(), "invalid line") { log.Fatalf("rewrite rules: %v", err) }

Prevention

When it happens

Trigger: A 'redirect match' line in the rewrite rules file (or passed to TestRedirectMatch/New) has missing or extra tokens — e.g. missing target, an unquoted URL containing spaces, or a stray trailing token.

Common situations: Hand-edited rewrite config files with typos, copy-pasted Apache-style directives with extra flags like [R=301], or target URLs that contain unencoded spaces.

Related errors


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