3

In my nginx config, I would like to catch /foo-bar/ and do a rewrite using location, but for it to ignore /foo-bar/anything-else.

I've tried:

location = /foo-bar/ {

and

location ~ ^/foo-bar/$ {

and

location ~ /foo-bar/ { 

but it also catches and rewrites

/foo-bar/?t=aaa-bbb-ccc

How do I catch only

/foo-bar/ 

and not

/foo-bar/?t=aaa-bbb-ccc  

?

Thanks.

Jaime Hablutzel
  • 456
  • 5
  • 10
Sean Lerner
  • 151
  • 1
  • 1
  • 5

2 Answers2

2

As per comment thread in cnst answer, I used:

if ($request_uri = "/foo-bar/") { rewrite ^ … permanent; }

and I never used

/location/
Sean Lerner
  • 151
  • 1
  • 1
  • 5
1
location = /foo/ {
    rewrite  ^  …  redirect;
}
location ~ /foo/ {
    return  404;
}

If this doesn't work, it must be the infamous permanent caching of the 301 Moved Permanently responses — try your request with curl -v to make sure it works or doesn't work, and/or clear the whole cache of your Gecko or other browser (in Gecko there is no other way around it (other than clearing the whole cache) — the Shift-refresh trick does not apply to 301).


Update: if you also need to ensure that no args are accepted (e.g. /foo/?test), then use the following:

location = /foo/ {
    if ($args) { return 403; }
    rewrite  ^  …  redirect;
}
cnst
  • 13,848
  • 9
  • 54
  • 76
  • Hi cnst. I tried the above config with `curl -v` and I am still being redirected. – Sean Lerner Mar 25 '13 at 16:58
  • No way, absolutely impossible. It'll only redirect from "/foo/", anything below "/foo/" will return a Not Found message; if that's not the case, then you must have something else you're not telling us about, or, perhaps, you are forgetting to restart nginx with the correct configuration. – cnst Mar 26 '13 at 15:59
  • Did you restart `nginx` after that? – cnst Mar 26 '13 at 17:21
  • Ya, I've been restarting nginx all along. – Sean Lerner Mar 26 '13 at 17:36
  • Hi @cnst, Sorry that last comment was accidentally posted (I've deleted it). The ? is tripping it up (as MartinFjordvald suggested above). The 404 returns as you suggested. Here are some test pages I've setup: http://tourdafrique.com/foo-bar/ _redirects to_ http://tourdafrique.com/redirected-to-test-page/ **and** http://tourdafrique.com/foo-bar/test _returns 404_ **and** http://tourdafrique.com/foo-bar/?test _redirects to_ http://tourdafrique.com/redirected-to-test-page/ – Sean Lerner Mar 26 '13 at 17:58
  • Well, you didn't tell us that! That's a completely different question now! – cnst Mar 26 '13 at 18:17
  • Anyhow, I've updated my answer, based on your new question. If it doesn't return 404 for `/foo/?`, and you still want it to (I don't understand what would be the usecase), then that one is also possible to catch, but you'd have to use `$request_uri`, and I don't really recommend going there. – cnst Mar 26 '13 at 18:23
  • what I'm aiming for is if there are any $args, then process the URL like normal, but if there aren't any $args, then do the redirect. It catches the URL without args using `if ($args = '')` but will deliver a 404 if there are not args. I've tried several statements after the if statement in the location block, but I can't get it to "continue on as normal". – Sean Lerner Mar 26 '13 at 20:19
  • a third totally different question? you have to be kidding us! place the following within your "like normal" location: `if ($request_uri = "/foo-bar/") { rewrite ^ … redirect;}` – cnst Mar 26 '13 at 21:00
  • cnst - `if ($request_uri = "/foo-bar/") { rewrite ^ … redirect;}` is the fix (though I used permanent instead `if ($request_uri = "/foo-bar/") { rewrite ^ … permanent;}`). My apologies for leading you down the 'location' path... I'm not experienced with nginx and I'm working with a config I didn't initially setup. – Sean Lerner Mar 26 '13 at 21:58