Help redirecting all urls with with a ?value after the url

Is it possible to use the _redirects file to redirect that looks like this so that all instances where there is anything after the domain with a ?q= the redirect takes the user to the homepage via a 301 or alternatively a 404?

I’ve tried doing this several different ways and I can’t seem to get it to work.

Taking inspiration from here, does the following work for you?

/ q=:q /?redir 301!

This will:

  • Run for requests made to / where a query parameter of q=[value] is passed
  • 301 redirect these requests to /?redir
    • You must pass a dummy query parameter key like ?redir (without a value is fine) because, by default, the query parameter from the request is automatically passed through
  • Force the rule (denoted by the !) and will therefore always execute the rule, regardless of whether any pages under /q=[value] exist

Hope this helps!

1 Like

That worked great thanks so much!