3

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).

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

all other links are just redirect 301 like 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

Sergej Proshe
  • 33
  • 1
  • 4
  • what does happen? – Gerard H. Pille Mar 13 '18 at 11:36
  • curl -I https://example.com HTTP/1.1 301 Moved Permanently Server: nginx Date: Tue, 13 Mar 2018 11:40:01 GMT Content-Type: text/html Content-Length: 178 Connection: keep-alive Location: https://newdomain.com/ curl -I https://example.com/some-location/test HTTP/1.1 301 Moved Permanently Server: nginx Date: Tue, 13 Mar 2018 11:40:22 GMT Content-Type: text/html Content-Length: 178 Connection: keep-alive Location: https://newdomain.com/some-location/test – Sergej Proshe Mar 13 '18 at 11:41
  • curl -I example.com/ ? BTW, what version of nginx? – Gerard H. Pille Mar 13 '18 at 11:44
  • curl -I https://example.com/ HTTP/1.1 301 Moved Permanently Server: nginx Date: Tue, 13 Mar 2018 11:50:55 GMT Content-Type: text/html Content-Length: 178 Connection: keep-alive Location: https://newdomain.com/ – Sergej Proshe Mar 13 '18 at 11:51
  • I did ask for "example.com/", not "example.com" – Gerard H. Pille Mar 13 '18 at 11:52
  • sorry, how i can format text on comment? nginx -v nginx version: nginx/1.12.2 curl -I https://example.com/ HTTP/1.1 301 Moved Permanently Server: nginx Date: Tue, 13 Mar 2018 11:50:55 GMT Content-Type: text/html Content-Length: 178 Connection: keep-alive Location: https://newdomain.com/ – Sergej Proshe Mar 13 '18 at 11:54
  • No formatting is possible in comments on Stackoverflow sites. Only in questions and answers. – Gerard H. Pille Mar 13 '18 at 11:55

1 Answers1

7

Your last "return 301" was overruling those inside the location.

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;
  }
}
Gerard H. Pille
  • 2,528
  • 1
  • 13
  • 17