nginx return adds trailing slash to url

Alexander McNulty asked:

This redirect adds a trailing slash which breaks the link I would like to spend people to, the exact code follows:

server {
    listen 80;
    server_name graphwhy.org;
    return 301 https://www.surveymonkey.com/r/nationaldebt$request_uri;
}

currently when someone enters graphwhy.org into their browser, they are redirected to https://www.surveymonkey.com/r/nationaldebt/ this url is not acknowledged. Survey monkey will only recognize https://www.surveymonkey.com/r/nationaldebt

could someone help me remove the trailing slash. Thank you in advanced.

My answer:


You asked for the URL path to be appended to the redirected URL, by using $request_uri. Thus, if you go to http://graphwhy.org/ you’re redirected to https://www.surveymonkey.com/r/nationaldebt/, because the requested URI is /. Further, if you go to http://graphwhy.org/because then you are redirected to https://www.surveymonkey.com/r/nationaldebt/because, because it’s /because.

If you just want people to be redirected to the same URL, regardless, then you should not have $request_uri here.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.