Friday, May 1, 2015

nginx: How to map a domain to another domain?



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?)



Answer



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 "~^(?.*\.)?example\.net$";
return 301 $scheme://${name}example.com$request_uri;
}



See this document for more.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...