kataras/iris · error

iris: rewrite: unexpected file extension:

Error message

iris: rewrite: unexpected file extension: 

What it means

rewrite.LoadOptions dispatches on the filename extension and only understands .yaml, .yml, and .json. Any other (or missing) extension hits the default branch and panics with 'iris: rewrite: unexpected file extension: ' + filename. The decoder is chosen purely from the extension.

Source

Thrown at middleware/rewrite/rewrite.go:56

func LoadOptions(filename string) (opts Options) {
	ext := ".yml"
	if index := strings.LastIndexByte(filename, '.'); index > 1 && len(filename)-1 > index {
		ext = filename[index:]
	}

	f, err := os.Open(filename)
	if err != nil {
		panic("iris: rewrite: " + err.Error())
	}
	defer f.Close()

	switch ext {
	case ".yaml", ".yml":
		err = yaml.NewDecoder(f).Decode(&opts)
	case ".json":
		err = json.NewDecoder(f).Decode(&opts)
	default:
		panic("iris: rewrite: unexpected file extension: " + filename)
	}

	if err != nil {
		panic("iris: rewrite: decode: " + err.Error())
	}

	return
}

// Rewrite is a struct that represents a rewrite engine for Iris web framework.
// It contains a slice of redirect rules, an options struct, a logger, and a domain validator function.
// It provides methods to create, configure, and apply rewrite rules to HTTP requests and responses.
//
// Navigate through _examples/routing/rewrite for more.
type Engine struct {
	redirects []*redirectMatch
	options   Options

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Rename the file to end with .yaml, .yml, or .json.
  2. If content is JSON, ensure the extension is .json, not .conf/.txt.
  3. Lowercase the extension if deploying to a case-sensitive filesystem.

Example fix

// before
rewrite.Load("redirects.conf") // panic: unexpected file extension
// after
rewrite.Load("redirects.yml")
Defensive patterns

Strategy: validation

Validate before calling

ext := strings.ToLower(filepath.Ext(rulesPath))
if ext != ".yaml" && ext != ".yml" && ext != ".json" {
    return fmt.Errorf("rewrite rules must be .yaml/.yml/.json, got %q", ext)
}

Try / catch

defer func() {
    if r := recover(); strings.Contains(fmt.Sprint(r), "unexpected file extension") {
        log.Fatalf("rewrite rules path bad extension: %v", r)
    }
}()
engine := rewrite.Load(rulesPath)

Prevention

When it happens

Trigger: Calling rewrite.Load("redirects.conf"), rewrite.Load("redirects") (no extension), or rewrite.Load("redirects.YAML") — since ext is taken verbatim after the last dot, uppercase extensions are not matched.

Common situations: Config files named .yml.txt after an editor save; extensionless config paths; using .jsonc or .toml formats; Windows-authored files with uppercase extensions deployed to Linux.

Related errors


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