0

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?

2
  • 1
    The ?f=105 is not part of the normalised URI used to match location statements. Try: if ($request_uri = "/forums/forumdisplay.php?f=105") { return 301 https://newurl; } Commented Dec 8, 2020 at 16:12
  • Thank you, this worked! I created a location just for /forums/forumdisplay.php and then used the if block inside. Commented Dec 8, 2020 at 16:18

1 Answer 1

1

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 
1
  • There was one important step missing here, that is, processing of requests where f query argument is not set. One has to ensure those requests are handled properly. What "properly" is, depends on the other configuration. Commented Dec 9, 2020 at 17:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.