Recently my website changes from [my-domain.com](https://my-domain.com) to [mydomain.com](https://mydomain.com)
I want to redirect everyone who is still using [my-domain.com](https://my-domain.com) to [mydomain.com](https://mydomain.com) to prevent any confusion.
This is easy enough for those visiting http://my-domain.com
server {
listen 80;
server_name _;
return 301 https://mydomain.com$request\_uri;
}
What about those visiting [https://my-domain.com](https://my-domain.com) though? The few times that I have almost gotten it to work it has issues due to the wrong certificate being issued.
In summary, when changing domains, how do I forward all of the http and https traffic to the new domain?
You can do it in the same block, you just need to define the certificate that still works for the ‘my-domain.com’ version:
server {
listen 80;
listen 443 ssl;
server_name _;
ssl_certificate /path/to/my-domain-cert.cert;
ssl_certificate_key /path/to/my-domain-cert.key;
return 301 https://mydomain.com$request_uri;
}
The overall method is the same: you do a 301 permanent redirect. You can even do it in a single server block:
server {
listen 80;
listen 443 ssl;
server_name my-domain.com;
return 301 https://mydomain.com$request_uri;
(SSL stuff)
}
However, to make people visiting https://my-domain.com not see an SSL warning, you need to have nginx serve a valid SSL/TLS certificate to https://my-domain.com visitors.
This is because the HTTPS negotiation happens _before_ the redirect, so you have to have a valid HTTPS configuration for your old domain for this to work right.
Put the SSL config where `(SSL stuff)` is in the example code above and you ought to be good to go.