Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx 301 redirect with query string

Tags:

redirect

nginx

Currently I have something like this in my nginx.conf file:

location ~ /old/page/?$ {
    return 301 /new-page;
}

The issue is that query strings are being stripped from the /old/page?ref=xx URL.

Is it possible to include query strings using the redirect method I'm using above?

like image 947
Tom Usborne Avatar asked Nov 25 '16 17:11

Tom Usborne


1 Answers

Anything from the ? and after is the query string and is not part of the normalised URI used in location and rewrite directives. See this document for details.

If you want to keep the query string, either add it to the return:

location = /old/page/ {
    return 301 /new/page$is_args$args;
}

Or with rewrite, the query string is automatically appended unless a ? is added:

rewrite ^/old/page/$ /new/page permanent;

See this document for location syntax, and this document for return/rewrite.

like image 98
Richard Smith Avatar answered Oct 18 '22 12:10

Richard Smith