Append prefix to uri

1. The problem I’m having:

I want to achieve the opposite behavior of “uri strip_prefix”: I always want to add a /foo in front of the url.

e.g.

[domain] → [domain]/foo
[domain]/a → [domain]/foo/a

2. Caddy version:

2.3.0

Found it: it’s just rewrite * /foo{uri}

1 Like

That’s a very old version. Please upgrade to the latest, v2.7.4. You should not wait so long to upgrade.

1 Like

That you for that hint! Just updated to the latest version!

But still struggling with my configuration: can I chain multiple rewrite / uri s?

e.g.

uri string_prefix /api
rewrite * /app/service{uri}

doesn’t seam to work. Neither does e.g.

rewrite * /app{uri}
rewrite * /service{uri}

I’m trying to achieve the following:

If a request starts with /api: strip /api and redirect to /app/service
Else redirect to /app/web

Here is my current setup, that doesn’t seam to work:

[myurl] {
route /api* {
uri strip_prefix /api
rewrite * /app/service{uri}
reverse_proxy [k8s-domain]
}

	# If this line exists, all request are redirected to the frontend (web)
    rewrite * /app/web{uri}
	
    reverse_proxy [k8s-domain]

}

handle_path /api* {
	rewrite * /app/service{uri}
}

handle {
	rewrite * /app/web{uri}
}

reverse_proxy wherever

Haha … Just came up with this solution some minutes ago - and works like a charm :slight_smile: Thank you very much anyway!

One more question, if I’m already here: I pretty much liked the short version you posted here (and had it first, too), but need some different header_up for the two cases. I now put the reverse_proxy in both of the handlers. Guess that’s ok, since they are terminating handlers?

Yeah, that’s fine :+1: