1

Nginx, I am trying to redirect one url matching the query parameter but it doesn't work.

http://www.example.org/xyz?abc=123&color="<K>" to the below URL http://www.example.org/anything/something).

I tried below but it didn't work.

location = /xyz {
    if ($args ~* "^&color="<K>") {
        rewrite ^.*$ /anything/something redirect;
    }
}

Anyone can help on it?

1 Answer 1

1

The characters such as ", <, and > are URL encoded in the original request and remain encoded in the $args variable.

Also, the ^& in your regular expression, anchors the & to the beginning of the query string, whereas there is actually another parameter in front of it.

The following example appears to work correctly:

location = /xyz {
    if ($args ~* "&color=%22%3CK%3E%22") {
        ...
    }
}

Alternatively, you can test the value of the color parameter directly using:

location = /xyz {
    if ($arg_color = '%22%3CK%3E%22') {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried for below also but didnt help out with below. example.org/xyz?abc=123&color=K to example.org/anything/something location = /xyz { if ($args ~* "&color=K") { rewrite ^.*$ /anything/something redirect; } }

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.