Mohammad Ali Akbari
Mohammad Ali Akbari

Reputation: 10395

Rename specific query parameter during redirect in nginx

I want redirect urls like /catalogsearch/result/?q=[keyword] to /products?keyword=[keyword]. Whats wrong with following rule?

location ~ /catalogsearch/result/?q=(.*) {
    rewrite ^ /products?keyword=$1 permanent;
}

Upvotes: 1

Views: 562

Answers (1)

Richard Smith
Richard Smith

Reputation: 49702

The ? and anything after it (the query string) is not part of the normalized URI used by the location and rewrite directives.

Assuming that there is only one parameter, you can access it using the $arg_ family of variables.

location = /catalogsearch/result/ {
    return 301 /products?keyword=$arg_q;
}

Upvotes: 1

Related Questions