CKR
CKR

Reputation: 27

Nginx redirect URL with specific query parameter on url

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?

Upvotes: 1

Views: 342

Answers (1)

Richard Smith
Richard Smith

Reputation: 49692

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') {
        ...
    }
}

Upvotes: 1

Related Questions