【问题标题】:NGINX Subdirectory Redirect is not workingNGINX 子目录重定向不起作用
【发布时间】:2017-10-27 18:29:37
【问题描述】:

TLDR: subdirectory/cool 重定向到 newdirectory/coolsubdirectory?cool 在没有参数的情况下错误地重定向到 newdirectory

问题:

我正在尝试将子目录重定向到新目录,同时使用正则表达式 ~* ^(?:((\/m\b)(?!-)(\/)?))(?<requestedPath>.*)?$ 通过 NGINX 位置保留原始 URL 参数

/subdirectory/cool等路径成功重定向到/newdir/cool。但是,/subdirectory?cool/subdirectory/?cool 等路径会重定向到 /newdir,而不保留 URL 参数。

重定向应该如下工作:

  • /子目录 -> /newdir

    (我在这里看到的许多关于子目录重定向的建议都假设尾随 slas 没有考虑到 /m 的正则表达式也会影响 /media)

  • /子目录/ -> /newdir/
  • /subdirectory/test -> /newdir/test
  • /子目录/?apple -> /newdir/?apple
  • /subdirectory?apple -> /newdir?apple

这里测试成功:Successful Test

失败的代码:

location ~* ^(?:((\/subdirectory\b)(?!-)(\/)?))(?<requestedPath>.*)?$ {
        return 301 /newdir/$requestedPath;
}

另外,我也尝试过使用重写而不是位置,但无济于事。

【问题讨论】:

    标签: nginx url-redirection nginx-location


    【解决方案1】:

    显然,有了位置,我们必须$is_args

    这成功了:return 301 /newdir/$requestedPath$is_args$args;

    最终结果:

    location ~* ^(?:((\/subdirectory\b)(?!-)(\/)?))(?<requestedPath>.*)?$ {
            return 301 /newdir/$requestedPath$is_args$args;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 2019-04-29
      • 2017-12-31
      • 2018-07-02
      • 2017-09-25
      • 2016-01-09
      相关资源
      最近更新 更多