I
I
Ilyas Salikhov2011-01-25 00:08:24
Nginx
Ilyas Salikhov, 2011-01-25 00:08:24

Nginx rewrite - convert address with parameters to NC address?

One of the sites used to have addresses with parameters like index.php?bar=foo&z=x. Now the site has been rewritten, CNC addresses, nginx is on the front.
It is necessary to register rewrites from old addresses to new ones so that the links on the Internet are not broken.
After reading, I found out that the parameter string is not passed to rewrite, but it is in the $query_string variable.
As the first option, I did this:

<pre>
if ($query_string = 'bar=foo&z=x') {
  rewrite ^(.*)$ http://site.ru/bar/foo/z/x/ permanent;
}
</pre>

But I came across the fact that the redirect actually goes to the page site.ru/bar/foo/z/x/?bar=foo&z=x , which causes this construction to fire again and loop.
Is there a more elegant solution?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Seleznev, 2011-01-25
@alkali

Regarding the addition of query_string, a quote from the documentation:

If arguments are specified in the replacement string, the previous query arguments are added after them. You can opt out of this addition by specifying a question mark at the end of the replacement string:

I
iDrum, 2011-01-25
@iDrum

sysoev.ru/nginx/docs/http/ngx_http_rewrite_module.html

If arguments are specified in the replacement string, the previous query arguments are added after them. You can opt out of this addition by adding a question mark at the end of the replacement line:
rewrite ^/users/(.*)$ /show?user=$1? last;

I
Ilyas Salikhov, 2011-01-25
@muxx

Yes, I was looking at it.
Thank you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question