简体   繁体   English

nginx将所有页面重定向301到另一个域/主页添加查询字符串

[英]nginx redirect 301 all pages to another domain / homepage add query string

I ask for help because I have already broken my head, I do not know how to solve the problem ... 我寻求帮助,因为我已经伤了脑筋,不知道如何解决问题...

I move from one domain to another domain, I want all links 301 redirect to to the new domain, but to the home page I want to add query string like ?from=example.com (only for homepage). 我从一个域移动到另一个域,我希望所有链接301都重定向到新域,但是到主页我想添加查询字符串,例如?from = example.com(仅用于主页)。

https://example.com 301 -> https://newdomain.com/?from=example.com https://example.com 301-> https://newdomain.com/?from=example.com

all other links are just redirect 301 like https://newdomain.com $request_uri; 所有其他链接只是重定向301,例如https://newdomain.com $ request_uri;

I tried so, but it does not work 我试过了,但是没有用

    server {
    listen 443 ssl;
    server_name  example.com www.example.com;

    location = / {
    return 301 https://newdomain.com/?from=example.com;
    }

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

Thanks for help 感谢帮助

Your last "return 301" was overruling those inside the location. 您上一次的“返回301”否决了该位置内的那些内容。

server {
  listen 443 ssl;
  server_name  example.com www.example.com;

  location = / {
    return 301 https://newdomain.com/?from=example.com;
  }

  location / {
    return 301 https://newdomain.com$request_uri;
  }
}

nginx -v nginx version: nginx/1.12.2 nginx -v nginx版本:nginx / 1.12.2

curl -I https://example.com/  

HTTP/1.1 301 Moved Permanently HTTP / 1.1 301永久移动
Server: nginx 服务器:nginx
Date: Tue, 13 Mar 2018 11:50:55 GMT 日期:2018年3月13日,星期二11:50:55 GMT
Content-Type: text/html 内容类型:text / html
Content-Length: 178 内容长度:178
Connection: keep-alive 连接:保持活动状态
Location: https://newdomain.com/ 位置: https//newdomain.com/

like that 像那样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM