Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename args in nginx redirect

Tags:

nginx

I am trying to get an NGINX redirect to take me from:

https://example.com/ui/?outletID=123&setID=456

to:

https://example.com/ui/?outletId=123&setIds=456

So outletID -> outletId AND setID -> setIds

IMPORTANT: I don't know where these params appear inside the URL, so there might be other strings before or after these. I only care for this replacements: outletID -> outletId; setID -> setIds.

This works at first try:

if ($args ~* "^outletID=(\d+)&setID=(\d+)(.*)") {
    set $outletid $1;
    set $setid $2;
    set $everythingelse $3;
    set $args '';
    rewrite ^.*$ /ui/?outletId=$outletid&setIds=$setid$everythingelse permanent;
}

But it looks like really bad practice and I particularly hate the $everythingelse ($3) solution I ended up with.

like image 731
VMold Avatar asked Dec 09 '25 19:12

VMold


1 Answers

To cover all possibilities, you probably need to do this in two stages, one redirect if outletID is present, and another redirect if setID is present.

For example:

if ($args ~ ^(.+&|)outletID(=.*)$) {
    return 301 "$uri?${1}outletId$2";
}
if ($args ~ ^(.+&|)setID(=.*)$) {
    return 301 "$uri?${1}setIds$2";
}
like image 52
Richard Smith Avatar answered Dec 11 '25 07:12

Richard Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!