Rewrite rule for react apps in Caddy v2

Hey, In Caddy v1 I had this to make my react apps work:

rewrite {
  if    {path} not_match ^\/0.0.0.0
  to    {path} {path}/ /?_url={uri}
}

What would this look like in Caddy’s v2 config format?

Firstly, I would recommend going through the upgrading to v2 docs here Upgrading to Caddy 2 — Caddy Documentation.

Thinking in v1 and trying to convert v1 config snippets directly is not the best approach, v2 is more powerful and you should be thinking of your config entirely in v2.

That snippet in v2 would be equivalent something like

@try_files {
	not path_regexp ^\/0.0.0.0
    file {
		try_files {path} {path}/ /?url={uri}
	}
}
rewrite @try_files {http.matchers.file.relative}

Hope that works for you.

Actually @abiosoft the try_files directive doesn’t support matchers. It’s essentially a shortcut for a file matcher with a rewrite.

Instead, it would look more like this:

@filter {
	not path_regexp ^\/0.0.0.0
	file {
		try_files {path} {path}/ /?url={uri}
	}
}
rewrite @try_files {http.matchers.file.relative}

See here:

Haha, noticed that later. Already updated the comment before noticing yours.

Thanks.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.