Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $args in nginx rewrite causing duplicate URL parameters

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?

like image 845
sirmarlo Avatar asked May 13 '14 15:05

sirmarlo


1 Answers

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.

like image 82
iBobo Avatar answered Nov 16 '22 02:11

iBobo