liuzhijun
liuzhijun

Reputation: 4469

nginx rewrite to no query string url

when access url with query string, I want to rewrite to no query string url, like:

/blog/(\d+)/(w+)?a=1$b=2....

rewrite to

/blog/(\d+)/(w+)

Upvotes: 0

Views: 977

Answers (1)

Hammer
Hammer

Reputation: 1582

If you would like to rewrite all requests a query string, just simply add these to your server block.

if ($query_string != "") {
    rewrite ^(.*)$ $uri? last;
} 

Explain

The syntax of rewrite directive is

rewrite regex replacement [flag];

First the if statement will match all requests with query strings and rewrite them with the replacement $uri?

According to the documentation, the query strings are dropped because

If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended.

Finally the last flag tells nginx to

stop processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;

Upvotes: 1

Related Questions