0

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+)

1 Answer 1

1

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;

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.