rubo77
rubo77

Reputation: 20865

Map all subdomains to another domain with nginx

With this sever config I can redirect all requests to another domain:

server {
    server_name example.net;
    listen [::]:80;
    listen 80;

    return 301 https://other.net$request_uri;
}  

but how can I redirect all subdomains to a new domain?

www.example.net     -->   www.other.net
webmail.example.net -->   webmail.other.net
forum.example.net   -->   forum.other.net

Can I use a placeholder in the return command?

Upvotes: 0

Views: 713

Answers (1)

Alexander Azarov
Alexander Azarov

Reputation: 13221

Use a regular expression in server_name:

server {
  server_name ~^(?P<subdomain>.+\.)example\.net$ ;
  listen [::]:80;
  listen 80;

  return 301 https://${subdomain}other.net$request_uri;
}

Please see this answer as there are few variants on specifying regular expressions in Nginx.

Upvotes: 2

Related Questions