Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx rule for NOT ignoring request parameters

Ok, I just want to add parameters whenever /search is called:

rewrite ^/search$ /search?foo=bar redirect;

Seems simple logic but it leads to endless loop because nginx takes the parameters separately and so the $ isn't working anymore. /search?foo=bar trigger it again and again and again because it stupidly matches.

So is there any working solution without touching the $args?

(I want to use regex because the matching rule is a bit more complicate but basically its what I want)

like image 523
Calmon Avatar asked Mar 09 '26 01:03

Calmon


1 Answers

According to the nginx wiki, "rewrite operates only on path, not parameters."

So maybe the key is to rewrite only if the parameter is not found.

    if ($args !~ "(^|&)foo=") {
        rewrite ^/search$ /search?foo=bar redirect;
    }
like image 100
slackwing Avatar answered Mar 10 '26 14:03

slackwing



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!