How check in nginx for query_string parameters?

14,999

Something like this might work. It says "if the query string contains the sequence 'q=' send a redirect". At least that's what I think it says, I'm not great with regex. I've tested it and it works, but there may be side effects depending what query strings you could get.

if ($query_string ~ q=) {
  return 301 "https://www.example.com";
}

This answer helped me work it out.

Share:
14,999

Related videos on Youtube

astropanic
Author by

astropanic

Monkey patching, Powerizing, Chess playing and long Emacs sessions...

Updated on September 18, 2022

Comments

  • astropanic
    astropanic over 1 year

    How I can write a condition in the nginx config testing if a q parameter was entered in the URL?

    This doesn't work:

     if ($arg_q) {
       return 301 "/someurl";
     }
    

    It catches URLs like

    /search?entered_search=1&q=123
    

    But it doesn't catch URLs like

    /search?entered_search=1&q=