2

I have foo.example and bar.example. I want sub.foo.example to redirect (not be aliased with) sub.bar.example, same for asdf.foo.example and asdf.bar.example, etc, for all sub domains.

I know I can redirect all subdomains of foo.example to a particular subdomain of bar.example (e.g. sub.foo.example & asdf.foo.example both go to www.bar.example) using a wildcard DNS record, but I am not aware of how to maintain the requested subdomain name and apply it with the redirect.

Is this even possible?

  • What webserver are you using? Do all these subdomains point to the same place? (ie. the main domain's document root?) "using a wildcard DNS record" - although that's not a "redirect". – MrWhite Jan 1 at 1:31
  • They're all going to be pointing to a NGINX setup. I just don't want to have to add extra configuration to the DNS every time I add a sub domain that NGINX cares about, if possible. – Tim Morris Jan 1 at 6:30
  • "configuration to the DNS" - for that you would need to configure a wildcard subdomain (every subdomain becomes available at the source domain). But your question is about "redirects", not DNS config? – MrWhite Jan 1 at 11:07
2

You have a misconception about what DNS can do. You say, "I know I can redirect all subdomains ... using a wildcard DNS record." However, DNS can't do redirects at all. All DNS can do is point your domain (or subdomains) to the IP address of a server. Even if you use a CNAME record, DNS doesn't redirect. A CNAME just means that the domain resolves to the same IP address as some other domain. The same webserver will handle the requests for both domains. It is up to the webserver whether they will be separate sites, or whether one will redirect.

Because DNS can't redirect, you can have all your subdomains resolve to a single server using a wildcard record. For the subdomains, you can either use an A record with the IP address of your server, or you can use a CNAME record with another host name. You can't use a CNAME record for the domain apex, so you will have to use an A record for that one.

A foo.example 192.168.1.1
A *.foo.example 192.168.1.1

OR

A foo.example 192.168.1.1
CNAME *.foo.example bar.example

OR

A foo.example 192.168.1.1
CNAME *.foo.example foo.example

Then you need to configure nginx to handle the requests and set up the desired redirects. Bart's answer to Redirecting a subdomain with a regular expression in nginx from Stack Overflow explains how to do that part:

server {
    server_name ~^(?<subdomain>\w+)\.foo\.example$;
    location / {
        rewrite ^ https://$subdomain.bar.example$request_uri permanent;
    }
}

The subdomain before foo.example is matched and stored in variable $subdomain which then can be used in the rewrite. This rewrites url like xxx.foo.example to xxx.bar.example with only one server directive.

You would also need a separate rule to redirect the bare domain:

server {
    server_name foo.example;
    location / {
        rewrite ^ https://bar.example$request_uri permanent;
    }
}
  • "You have a misconception about what DNS can do." Probably. Haha. Networking is not my specialty! Thank you for the in-depth reply. – Tim Morris Jan 2 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.