nginx 301 redirect with query string

2019-02-09 07:00发布

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?

1条回答
爷的心禁止访问
2楼-- · 2019-02-09 07:37

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.

查看更多
登录 后发表回答