简体   繁体   中英

nginx rewrite part of url with ? mark

I'm serving a website with nginx as a proxy for apache. I have a problem with rewrite module.

http://example.com/foo/bar/url?someparam=1&otherparam=2

I need to rewrite from

/foo/bar/ url ?someparam=1&otherparam=2

to

/foo/bar/ url2 ?someparam=1&otherparam=2

I found it problematic, since it's not /foo/bar/url/someparam , but it's only a part of text, i need to rewrite in dynamic url's.

I was trying: rewrite ^(.*)url(.*)$ $1url2$2; but i'm getting error 500 on that.

How can i do that?

In your rewrite statement, the new URI also matches the regular expression pattern. This will cause a redirection loop that ends with a 500 response.

You need to make the regular expression more specific so that the new URI does not match.

Also, the nginx uses a normalised URI when processing rewrite statements, that does not include the ? and the query string that follows it. So your statement could be simplified as follows:

rewrite ^(.*)url$ $1url1;

The rewrite directive will automatically append any ? and query string in the original URI.

See this document for more.

The URI that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM