将HTTPS域重定向到新的HTTPS域

时间:2019-05-08 06:38:54

标签: url nginx redirect server url-rewriting

须知:

我使用Vultr.com(类似于DigitalOcean)作为我的VPS,并在Nginx和Ubuntu 18.10 x64上运行1台服务器。

我有2个正在使用的域:

  • https://(www).oldsite.org
  • https://(www).newsite.org
  • (www)=有无www前缀
  • 两个域都具有SSL证书

我的问题:

当前https://(www)oldsite.org正在镜像我的https://newsite.org

如果我删除了https://并仅转到(www)oldsite.org,它将重定向到https://newsite.org且没有问题。

只有当我添加https://时才会发生镜像。

我的问题:

如何解决我的网站可用脚本将oldsite重定向到newsite

我需要以下内容进行重定向:

  • https://www.oldsite.org重定向到https://newsite.org
  • https://oldsite.org重定向到https://newsite.org

我的脚本:

server {
root /var/www/newsite/public;
 server_name newsite.org www.newsite.org oldsite.org www.oldsite.org;

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/newsite.org-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/newsite.org-0001/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {

    if ($host = www.newsite.org) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = newsite.org) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = oldsite.org) {
        rewrite ^ https://newsite.org$request_uri? permanent;
    } # managed by Certbot


    if ($host = www.oldsite.org) {
        rewrite ^ https://newsite.org$request_uri? permanent;
    } # managed by Certbot


    listen 80;
    listen [::]:80;
    server_name newsite.org www.newsite.org oldsite.org www.oldsite.org;
    return 404; # managed by Certbot
}

而不是这样做:

    if ($host = oldsite.org) {
        rewrite ^ https://newsite.org$request_uri? permanent;
    } # managed by Certbot


    if ($host = www.oldsite.org) {
        rewrite ^ https://newsite.org$request_uri? permanent;
    } # managed by Certbot

是否可以使用类似于Wildcard的方法:

//NOTE THE * before oldsite.org

    if ($host = *oldsite.org) {
        rewrite ^ https://newsite.org$request_uri? permanent;
    } # managed by Certbot

如果我可以输入任何可以输入重定向到https://newsite.org

的前缀,那将是理想的选择

希望涵盖所有基础,并提供这些oldsite网址重定向的各种变化。

  • https://oldsite.org
  • https://www.oldsite.org
  • www.oldsite.org
  • oldsite.org

谢谢!

1 个答案:

答案 0 :(得分:1)

首先,我建议您为不同的站点使用单独的配置。更容易管理。

我猜这种脚本变体可以解决您的问题:

server {
     listen 80;
     listen [::]:80;
     server_name oldsite.org www.oldsite.org;
     return 301 https://newsite.org$request_uri;
}

server {
     listen 443 ssl;
     listen [::]:443 ssl;
     server_name oldsite.org www.oldsite.org;
     return 301 https://newsite.org$request_uri;

     ssl_certificate /etc/letsencrypt/live/newsite.org-0001/fullchain.pem; # managed by Certbot
     ssl_certificate_key /etc/letsencrypt/live/newsite.org-0001/privkey.pem; # managed by Certbot
     include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}