0

I'm writing an application to provide read-only access to a number of legacy applications. I want to use Nginx to redirect from the old URLs to a single new URL like this:

Old URL: http://oldsite.com/?value=something

New URL: http://newsite.com/?value=something&source=oldsite

Here's my Nginx server block for oldsite.com:

server {
    listen 80;
    listen 443 ssl;
    server_name oldsite;
    return 301 $scheme://newsite.com$request_uri&source=oldsite;
}

This works perfectly - put in the oldsite.com URL and you're redirected to the correct newsite.com URL.

But I'm confused by the following behaviour:

Enter http://oldsite.com in the browser

I expect to be redirected to http://newsite.com&source=oldsite.

Instead, I'm redirected to http://newsite.com.

I think what might be happening is this: when the $request_uri is empty or null, Nginx stops processing the string and returns whatever it has.

While this is what I want, it is not what I expected. I have not been able to find any mention of this in the Nginx documentation and I don't want to rely on an undocumented "feature".

Can anyone explain what's going on here?

3
  • 1
    Try running the request with wget to see the actual redirect. The URL you are returning is invalid if the request doesn't contain any parameters (parameters are separated by the path with a ?). I could imagine that the client drops everything from the first unexpected character (&) Mar 28 '18 at 18:04
  • Great suggestion! Interestingly, I get a 500 error when I do that because it's redirecting to http://newsite.com/&source=oldsite. Copying this URL to the browser (chrome) also results in the 500 error. But when the browser gets the 301 response directly, it appears to do what you suggest, removing everything from the & symbol on.
    – Kryten
    Mar 28 '18 at 18:12
  • 2
    You should consider using rewrite instead of return as it handles optional parameters correctly. E.g. rewrite ^ $scheme://newsite.com$uri?source=oldsite permanent; Mar 28 '18 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.