28

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 Answer 1

62

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.