1

After spending hours using duckduckgo, this was the best answer I arrived at and it's still not working to redirect a currently HTTPS domain to a new HTTPS domain.

server {
    listen 80;
    listen 443 ssl;
    server_name www.olddomain.com olddomain.com;
    rewrite 301 https://newdomain.com$request_uri;
}

Browser gives Insecure Connection error.

I have also tried things like

server {
    listen 443 ssl;
    server_name olddomain.com;
    ssl on;
    ssl_certificate /etc/ssl/certs/OLD.crt;
    ssl_certificate_key /etc/ssl/private/OLD.key;

    #enables all versions of TLS, but not SSLv2 or 3 which are weak and now deprecated.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    ssl_ciphers "ALLLLLLTTHHISSSS";
    ssl_prefer_server_ciphers on;

    rewrite 301 https://newdomain.com$request_uri;
}

Although this option stops giving an error the rewrite is not working and it goes to a "Welcome to NGINX Page."

1
  • 1
    return 301 not rewrite. SSL connects first, you need a valid SSL even to redirect... Look at letsencrypt Nov 14, 2017 at 22:10

3 Answers 3

3

If you want to redirect https requests from an old to a new domain name you'll need a certificate for the old website configured inside that Nginx server block.

You'll need to cater for https://example.com and https://www.example.com

This is a basic config that should do the job. IMHO you don't need to worry about SSL versions for a server that just does forwarding, just use the defaults.

server {    
  server_name www.example.com;    

  listen 443 ssl;    

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;    
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;    

  return 301 https://www.example2.com$request_uri;          
}    
2
  • This will give Cert Error if 1. the domains are on the same host/IP 2. the certs don't support both the old and new domain The only way the above may work is if the old domain is on a different IP/Host to the new one. For a solution that works with the first scenario see this: serverfault.com/questions/633168/… Nov 24, 2021 at 13:30
  • This works across domains on my own server. I haven't tried cross servers so you may be correct.
    – Tim
    Nov 24, 2021 at 17:02
1

When I need to redirect an entire domain, I usually just forward the domain at the domain name registrar level. Here's how to forward your domain if you're using GoDaddy. Most registrars will allow you to do this.

Benefits of setting forwarding at registrar level:

  • You don't have to worry about getting things right on your server
  • You won't accidentally forget to set up your Apache (or other web server) redirects if you move to a new server

Drawbacks to server-level forwarding:

  • Less control if you're just forwarding say port 80 but not 443

Sometimes the simplest answer is best... took me a while to think of this the first time :)

3
  • I don't think using registrar specific solutions is a great idea.
    – Tim
    Nov 14, 2017 at 22:58
  • @tim, can you provide your specifics on why registrar forwards are not a good idea? I already noted some of them but will add more if they are not just personal preference. Nov 14, 2017 at 23:28
  • Vendor specific solutions can create unexpected and difficult to solve problems.
    – Tim
    Nov 15, 2017 at 2:30
0

If you have a 301 redirect on the new domain from http to https you can avoid the certificate problem by redirecting from the old-domain to the http version of the new domain. eg:

On the old domain

server {
    server_name old-domain.com www.old-domain.com;
    return 301 http://new-domain.com/$request_uri;
}

On the new domain

server {
    listen 80
    server_name new-domain.com www.new-domain.com;
    return 301 https://new-domain.com/$request_uri;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .