9

In one of my location rules, I am trying to rewrite the URL as such:

rewrite ^ $topicredirecturi?$args permanent;

$topicredirecturi is calculated in a map file, mapping for example a URL such as

http://www.topics.com/companies/cit-group-inc/index.html

to

http://www.topics.com/companies/cit_group_inc/index.html

When I make my request with URL parameters such as:

http://www.topics.com/companies/cit-group-inc/index.html?rss=1

I get the following rewritten URL with duplicate params:

http://www.topics.com/companies/cit_group_inc/index.html?rss=1&rss=1

Similarly, the URL

http://www.topics.com/companies/cit-group-inc/index.html?rss=1&bob=2

gets rewritten to

http://www.topics.com/companies/cit_group_inc/index.html?rss=1&bob=2&rss=1&bob=2

Anyone know what might be going on here?

1 Answer 1

20

Nginx appends the query string automatically if there are other get parameters in the rewritten URL. If you're adding $args yourself you should add a ? (question mark) at the end of the rewritten URL to avoid having duplicate parameters.

The correct way of doing a redirect with query string is this:

rewrite ^(...)$ /destination$is_args$args? [permanent|redirect];

where $is_args contains a ? iff the query string is not empty, and $args is the query string itself; the question mark at the end requests to nginx to not add the query string again.

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.