Rewrite for depreciated domains

Hello all! I have a seemingly simple need, but I’m not sure the best way to implement it.

For some time, I used a domain for some of my services, let’s call it olddomain . com. I have since moved all the services to the new domain, called newdomain . com. Services are accessed via a unique subdomain, such as app1 . olddomain . com.

The need is simple, to allow users to use the old domain name, but redirect to the new domain. But I want to do this while preserving the subdomain the user typed in.

By way of example, if a user goes to app1 . olddomain . com, I’d like the browser to get redirected to app1 . newdomain . com. I think rewrite is the best way to implement this, but I’ve never gotten this deep into the caddyfile syntax. Can anyone help me out?

Hi @jiru443, try something like this:

*.olddomain.example {
  redir https://{label1}.newdomain.example{uri}
}

https://caddyserver.com/docs/redir
https://caddyserver.com/docs/placeholders

Placeholders! That’s the missing part. I’ll give it a try and let you know. Thanks!

This worked perfectly. Now I’m reading up on the docs to make an IF statement to not rewrite subdomain, www.

I’ve had success with hacky usage of rewrite in the past for conditional purposes, but in this case it’s probably simpler just to use another site definition.

You can override the www subdomain with its own behaviour. Caddy matches the most specific site when determining how to handle a request.

*.olddomain.example {
  redir https://{label1}.newdomain.example{uri}
}

www.olddomain.example {
  # non-redir behaviour here
  # (serve a site maybe?)
  # root /path/to/olddomain
}

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