Use $ args in Nginx rewrite to cause duplicate URL parameters

In one of my location rules, I tried to rewrite the URL:

rewrite ^ $topicredirecturi?$args permanent ;

$topicredirecturi is calculated in the mapping file, mapping such as URL, etc.

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

to

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

When I make a request using URL parameters, for example:

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

I get the following rewritten URL with duplicate parameters:

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

Similarly, URL

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

Rewritten as

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

Does anyone know what will happen here?

@Yirkha’s answer is wrong. Nginx does not automatically append the query string, but only It will only be appended when there are other get parameters in the written URL. If you add $args yourself, should you add one? (Question mark) at the end of the rewritten URL to avoid duplicate parameters.

The correct way to redirect using a query string is:

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

What does $is_args contain? The iff query string is not empty, and $args is the query string itself; the last question mark asks nginx to no longer add the query string.

In one of my location rules , I tried to rewrite the URL:

rewrite ^ $topicredirecturi?$args permanent;

$topicredirecturi is calculated in the mapping file, mapping for example URL etc.

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

to

< p>

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

When I make a request using URL parameters, for example:

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

I get the following rewrite with duplicate parameters URL:

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

Similarly, URL< /p>

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

is rewritten as

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

Does anyone know What will happen here?

@Yirkha answer is wrong. Nginx does not automatically append the query string, but only appends if there are other get parameters in the rewritten URL .If you add $args yourself, should you add one? (Question mark) at the end of the rewritten URL to avoid duplicate parameters.

The correct way to redirect using a query string is:

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

What does $is_args contain? The iff query string is not empty, and $args is the query string itself; the final question mark requests that nginx no longer add the query string.

Leave a Comment

Your email address will not be published.