Rewrite to another domain

Hi,

Setting up a local website, pretty simple like so:

localhost:2001 {
  root ./encollab.dp.wty.enquiry/

}

I’m trying to redirect all traffic to a particular part of the site to another domain entirely. The URL will look something like:

http://localhost:2001/sap/opu/odata/sap/[from here on anything goes]

So i tried a few things in this vein:

rewrite /sap/opu/odata/sap https://domain.com:port/sap/opu/odata/sap/{uri}

What would be the approach to redirect everything following /odata/sap/?

Hi @jorgt,

rewrite handles URI changes internally and invisibly to the client. Since you actually want to bump them to a different domain, you’ll want to use redir instead.

https://caddyserver.com/docs/redir

Since in normal usage, from must be exact, but you want to redirect under a base path instead, you’ll have to use if functionality like this to check for the base path yourself:

redir {
  if {path} starts_with /foo/bar
  / https://example.com:443/foo/bar{uri}
}

Alternately, you could self-contain it within its own site definition, which surprisingly requires fewer lines:

localhost:2001/foo/bar {
  redir / https://example.com:443{uri}
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.