【问题标题】:nginx redirect / rewrite issue on specific rule关于特定规则的 nginx 重定向/重写问题
【发布时间】:2016-11-01 19:26:13
【问题描述】:

我有一个 nginx 情况,需要一些帮助。

故事:

  • 曾经有一个网站,我们称之为 website.com,其中创建了虚拟子域。 subdomain.website.com 等每个 URL 都是动态创建的(有 subdomain.website.com、anothersubdomain.website.com、someothersubdomain.website.com 等)
  • 每个创建的子域都有一个内部结构,例如 subdomain.website.com/some/internal/structure/subdomain.website。 com/some-file.html
  • 现在,所有者想要永久移动主网站内的所有子域,例如这个 website.com/sub-subdomain/ 或 website.com/sub-subdomain/some/internal/structure 或 website.com/sub-subdomain/some-file.html

我现在掌握的是捕获子域并将它们重定向到主网站内的规则,如下所示:

if ($http_host !~ "^www.website.com"){
    set $rule_0 1$rule_0;
}

if ($http_host ~ "([^.]+).website.com"){
    set $rule_0 2$rule_0;
    set $bref_1 $1;
}

if ($rule_0 = "21"){
    rewrite  ^/(.*)  http://www.website.com/sub-$bref_1/ permanent;
}

问题是,如果我有一个像 subdomain.website.com/some/internal/structure/subdomain.website 这样的 URL .com/some-file.html 我用上面的代码 sn-p 得到的只是这个 www.website.com/sub-subdomain/ 我失去了子域结构的其余部分(“some/internal/structure/”或“some-file.html”部分 - 当然没有引号)

我应该如何更改上面的代码 sn-p 以便保留上面提到的丢失的部分?

提前感谢您的帮助!

【问题讨论】:

    标签: nginx url-rewriting


    【解决方案1】:

    好的,与此同时,我想出了这个解决方案:

    上面的代码sn-p我已经取消了,换成这个了

    if ($http_host ~ "([^.]+).website.com") {
        set $bref_1 $1;
    }
    
    if ($bref_1 != "www") {
        return 301 http://www.website.com/sub-$bref_1$request_uri;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      相关资源
      最近更新 更多