caddyserver/caddy · error

replacement for query field '%s': %v

Error message

replacement for query field '%s': %v

What it means

Raised by queryOpsReplacement.Provision when the search_regexp for a query replace operation fails to compile; the message names the query field ('key') whose regex is invalid. It is the inner error that 522 wraps. Compilation uses Go's regexp (RE2), so PCRE-only syntax is unsupported.

Source

Thrown at modules/caddyhttp/rewrite/rewrite.go:555

	Set []queryOpsArguments `json:"set,omitempty"`

	// Adds query parameters; does not overwrite an existing query field,
	// and only appends an additional value for that key if any already exist.
	Add []queryOpsArguments `json:"add,omitempty"`

	// Replaces query parameters.
	Replace []*queryOpsReplacement `json:"replace,omitempty"`

	// Deletes a given query key by name.
	Delete []string `json:"delete,omitempty"`
}

// Provision compiles the query replace operation regex.
func (replacement *queryOpsReplacement) Provision(_ caddy.Context) error {
	if replacement.SearchRegexp != "" {
		re, err := regexp.Compile(replacement.SearchRegexp)
		if err != nil {
			return fmt.Errorf("replacement for query field '%s': %v", replacement.Key, err)
		}
		replacement.re = re
	}
	return nil
}

func (q *queryOps) do(r *http.Request, repl *caddy.Replacer) {
	query := r.URL.Query()
	for _, renameParam := range q.Rename {
		key := repl.ReplaceAll(renameParam.Key, "")
		val := repl.ReplaceAll(renameParam.Val, "")
		if key == "" || val == "" {
			continue
		}
		if key == val {
			continue
		}
		originalValues, ok := query[key]

View on GitHub (pinned to 50e54ee279)

Solutions

  1. Check the 'key' named in the error and inspect its search_regexp
  2. Fix the RE2 syntax error shown in the wrapped error text
  3. If no regex is needed, clear search_regexp and use search_replace string matching instead
  4. Validate the config before deploy

Example fix

// before
{ "key": "utm_source", "search_regexp": "(?!google)", "replace": "x" }

// after (no lookahead in RE2)
{ "key": "utm_source", "search_regexp": "^google", "replace": "x" }
Defensive patterns

Strategy: validation

Validate before calling

func validQueryRegex(key, pattern string) error {
    if pattern == "" {
        return nil
    }
    _, err := regexp.Compile(pattern)
    if err != nil {
        return fmt.Errorf("query field %s: %w", key, err)
    }
    return nil
}

Prevention

When it happens

Trigger: rewrite -> query -> replace entry with search_regexp such as "a{1," (bad quantifier) or "\" (trailing backslash).

Common situations: Rewriting query parameters (e.g. stripping tracking params) with a hand-written regex that has a typo or unsupported construct.

Related errors


AI-assisted analysis of caddyserver/caddy@50e54ee279 (2026-08-15). Data as JSON: /api/errors/b7a79438d667cc72. Report an issue: GitHub.