1

I have a domain example.com and the server block looks something like this (it's a WordPress multisite):

server {
  server_name example.com www.example.com blog.example.com marketing.example.com;

  # other configurations
}

HTTPS is also set up with certbot (in case that matters).

Now I have another domain example.net and I want it to redirect to example.com (including all subdomains). For example, blog.example.net should redirect to blog.example.com. Is there a recommended way/pattern to achieve this in the nginx conf?

(Or, should be managed by the DNS in the VPS?)

thameera
  • 113
  • 4

1 Answers1

3

Create a dedicated server block with a regular expression server_name to capture the subdomain of example.net (if any) and use it in a return statement.

server {
    ...
    server_name "~^(?<name>.*\.)?example\.net$";
    return 301 $scheme://${name}example.com$request_uri;
}

See this document for more.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29