【问题标题】:If subdomain else than www., redirect to www如果子域不是 www.,则重定向到 www
【发布时间】:2016-06-16 15:20:08
【问题描述】:

在 nginx 配置中我添加了

server_name *.example.com example.com;

如果我只写

server_name www.example.com example.com;

nginx m.example.com 正在重定向到 nginx 配置中配置的第一个网站。

在第一行中,无论使用什么子域,我的网站都会在 rails 上显示相同的结果:m.example.com/hellowww.example.com/hello 等。没关系,但应该重定向到 www。

所以我需要重定向所有其他子域 => m.example.com/hello to www.example.com/hello 因为 SEO。如何从routes.rb 进行这种重定向?


现在我只使用规则从无子域example.com/* to www.example.com/*重定向

constraints subdomain: false do
  get ':any', to: redirect(subdomain: 'www', path: '/%{any}'), any: /.*/
end

【问题讨论】:

    标签: ruby-on-rails redirect nginx


    【解决方案1】:

    如果您愿意,您可以直接在 Nginx 中执行此操作。

    server_name *.example.com example.com;
    
    if ($host != 'www.example.com') {
        rewrite ^ http://www.example.com$request_uri? permanent;
    }
    

    删除永久标志以阻止这是 301 重定向。

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2015-10-16
      • 2018-06-02
      • 2013-09-18
      相关资源
      最近更新 更多