nginx重写正则重写root和subdirectory

发布于 2025-02-11 00:21:05 字数 396 浏览 1 评论 0原文

目前,我有

server {
    listen 8080;
    server_name default;

    location /foo/ {
        rewrite ^/foo(/.*)$ $1 break;
        proxy_redirect https://example.com https://$host/;
    }

这种匹配的foo,并且基本上剥离了它。我需要维护此功能,还需要包括bar(如果是foo之后)。 IE如果URL为/foo/bar,我希望这两个本质上都被剥离。我不太擅长于等级,并且正在努力做到这一点。

我在思考(foo)|(\/bar)之类的东西,但不确定如何将尾随的斜线与之匹配。

Currently i have

server {
    listen 8080;
    server_name default;

    location /foo/ {
        rewrite ^/foo(/.*)$ $1 break;
        proxy_redirect https://example.com https://$host/;
    }

This matches foo and essentially strips it. I need to maintain this functionality, and also include bar if it is after foo. i.e. if the url is /foo/bar I want both of those essentially stripped. I'm not great at regex and am struggling with the proper way to do this.

I'm thinking something like (foo)|(\/bar), but not sure how to match a trailing slash with that as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2025-02-18 00:21:06

/bar作为可选组件添加到您的正则模式:

rewrite ^/foo(/bar)?(/.*)$ $2 break;

请注意,捕获组引用已更改为$ 2

Add /bar as an optional component to your regex pattern:

rewrite ^/foo(/bar)?(/.*)$ $2 break;

Note that the capture group reference has been changed into $2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文