How to redirect to a different domain using Nginx?

server_name supports suffix matches using .mydomain.example syntax:

server {
  server_name .mydomain.example;
  rewrite ^ http://www.adifferentdomain.example$request_uri? permanent;
}

or on any version 0.9.1 or higher:

server {
  server_name .mydomain.example;
  return 301 http://www.adifferentdomain.example$request_uri;
}

Leave a Comment