Nginx Redirect for Specific URL

For our site's server block in nginx, I have these location directives:

location ~* ^.+\.(ico|css|js|eot|woff|otf|svg|gif|jpe?g|png|swg|woff2)(\?[a-z0-9=_]+)?$

location /applications/

location /

location ~ \.php$

I have a specific URL from our old forum software that I want to redirect. The URL looks like this:

https://www.example.com/forums/forumdisplay.php?f=105

So far, nothing I've tried seems to match this URL. I have tried a few different rewrite statements in the / location and I've also tried an exact match thinking that the longest match should win:

location = /forums/forumdisplay.php?f=105 {
    return 301 https://newurl;
}

This doesn't work - I still get a 404 from this URL. Where/how should I do this redirect?


One approach is to use:

location = /forums/forumdisplay.php {
    if ($arg_f = 105) {
        return 301 https://newurl;
    }
    fastcgi_pass /path/to/php.sock; # Send the request to PHP processor
}

This approach will match the /forums/forumdisplay.php URL where the f query argument is set to 105, and there can be additional query arguments.

The example in comment requires an exact match to the full URL, and doesn't work if URL happens to be

http://example.com/forums/forumdisplay.php?f=105&fbclid=4567