UPD: подставлять $uri в редирект это весьма плохая идея.
Третий пункт невозможен по определению.
path
в запросе есть всегда.
Для остального вот улучшенный вариант решения от
AUser0
map $uri $clean_uri {
default $uri$is_args$args;
"~(?'_'.+)/$" $_$is_args$args;
}
server {
if ($request_uri != $clean_uri) {
return 302 $clean_uri;
}
}
Примеры:
$ curl -s -o /dev/null -w '%{http_code}: %{redirect_url}\n' http://localhost:7777///foo
302: http://localhost:7777/foo
$ curl -s -o /dev/null -w '%{http_code}: %{redirect_url}\n' http://localhost:7777/foo//bar/
302: http://localhost:7777/foo/bar
$ curl -s -o /dev/null -w '%{http_code}: %{redirect_url}\n' http://localhost:7777///foo?q=42
302: http://localhost:7777/foo?q=42
$ curl -s -o /dev/null -w '%{http_code}: %{redirect_url}\n' http://localhost:7777/foo//bar/?q=42
302: http://localhost:7777/foo/bar?q=42
$ curl -s -o /dev/null -w '%{http_code}: %{redirect_url}\n' http://localhost:7777/foo
200:
$ curl -s -o /dev/null -w '%{http_code}: %{redirect_url}\n' http://localhost:7777/foo?q=42
200: