location redirect always with trailing slash... sometimes

Hans Schou hsc at miracle.dk
Fri Mar 8 08:58:19 UTC 2019


I found a solution (after reading the manual)
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
On Thu, 7 Mar 2019 at 14:57, Hans Schou <hsc at miracle.dk> wrote:

> Example of required redirect:
> http://ex.org/foo -> https://ex2.org/foo/    # Nx solves the bug here
> http://ex.org/foo/ -> https://ex2.org/foo/
> http://ex.org/foo/?id=7 -> https://ex2.org/?id=7
>

"rewrite" is the way to go.
To change /foo or /foo/ to /foo/ and don't change the rest, this will do it:
location ~ /(foo|bar) {
    rewrite ^(/[^/]+)/? https://ex2.org$1/ permanent;
}

If any path should be handled this way:
location / {
    rewrite ^(/[^/]+)/? https://ex2.org$1/ permanent;
}

-- 

Venlig hilsen - best regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20190308/14a5e536/attachment.html>


More information about the nginx mailing list