Nginx [SOLVED]: nginx rewrite part of url with ? mark

Nginx [SOLVED]: nginx rewrite part of url with ? mark

Home Forums Nginx Nginx [SOLVED]: nginx rewrite part of url with ? mark

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36724

    Anonymous

    QuestionQuestion

    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?

    #36725

    Anonymous

    Accepted AnswerAnswer

    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

    Source: https://stackoverflow.com/questions/47996862/nginx-rewrite-part-of-url-with-mark
    Author: Richard Smith
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.