4

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= 
astropanic
  • 307
  • 2
  • 5
  • 18

1 Answers1

4

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.

Tim
  • 31,888
  • 7
  • 52
  • 78