【问题标题】:nginx redirect 301 all pages to another domain / homepage add query stringnginx将301所有页面重定向到另一个域/主页添加查询字符串
【发布时间】:2018-08-21 15:07:31
【问题描述】:

求救是因为我的脑袋已经断了,不知道怎么解决……

我从一个域移动到另一个域,我希望所有链接 301 重定向到新域,但到主页我想添加查询字符串,如 ?from=example.com(仅用于主页)。

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

所有其他链接都只是重定向 301,例如 https://newdomain.com$request_uri;

试过了,还是不行

    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;
}

感谢您的帮助

【问题讨论】:

  • 发生了什么?
  • curl -I example.com HTTP/1.1 301 永久移动服务器:nginx 日期:2018 年 3 月 13 日星期二 11:40:01 GMT 内容类型:文本/html 内容长度:178 连接: keep-alive 位置:newdomain.com curl -I example.com/some-location/test HTTP/1.1 301 永久移动服务器:nginx 日期:2018 年 3 月 13 日星期二 11:40:22 GMT 内容类型:文本/html 内容长度:178 连接:保活位置:newdomain.com/some-location/test
  • curl -I example.com/ ?顺便说一句,什么版本的 nginx?
  • curl -I example.com HTTP/1.1 301 永久移动服务器:nginx 日期:星期二,2018 年 3 月 13 日 11:50:55 GMT 内容类型:文本/html 内容长度:178 连接:保活位置:newdomain.com
  • 我确实要求的是“example.com/”,而不是“example.com”

标签: linux redirect nginx web http-status-code-301


【解决方案1】:

您的上一个“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;
  }
}

【讨论】:

    【解决方案2】:

    nginx -v nginx版本:nginx/1.12.2

    curl -I https://example.com/  
    

    HTTP/1.1 301 永久移动
    服务器:nginx
    日期:格林威治标准时间 2018 年 3 月 13 日星期二 11:50:55
    内容类型:text/html
    内容长度:178
    连接:保持活动
    地点:https://newdomain.com/

    这样

    【讨论】:

    • 试试我提交的答案
    猜你喜欢
    • 2013-10-24
    • 2013-08-06
    • 2014-02-17
    • 2011-04-26
    • 2016-02-11
    • 1970-01-01
    • 2014-09-23
    • 2014-06-13
    • 2013-02-24
    相关资源
    最近更新 更多