Caddy 2 multidomains rewrite

Hello, I’m developing a multidomain site using Django and Caddy 2(with Cloudflare plugin)
The project has about 20 domains:
project.app
project.bot
project.live
project.news
project.wiki
and so on…
I wanna catch requests to domains and rewrite them to some project.dev/wiki/ project.dev/news/ etc…
What you think is better way of doing this?

Assuming the TLD maps 1-to-1 to the subfolder you want, you could do something like this:

example.dev,
example.app,
example.bot,
example.live,
example.news,
example.wiki {
  @secondary {
    not host example.dev
  }
  rewrite @secondary /{http.request.host.labels.0}{uri}

  # rest of the site config goes here
}

Then requests to example.live will be treated as a request to example.dev/live. A request like example.live/foo/bar will be treated as example.dev/live/foo/bar.

2 Likes

Thanks, I’ll try this. I even didn’t seen these {http.request.host.labels.0} in docs.

Aye, I actually meant to put some docs references down! I just forgot :joy:

It’s a little less discoverable if you’re a Caddyfile-only user (i.e. avoiding the JSON) - it’s documented in the http app itself: Modules - Caddy Documentation

1 Like

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