【问题标题】:Add WWW to URL when it's not present on Ruby on Rails在 Ruby on Rails 上不存在时将 WWW 添加到 URL
【发布时间】:2015-12-04 07:54:16
【问题描述】:

当我的网址不存在时,我想强制我的网址以www. 开头。

我尝试了 match 方法,但它似乎忽略了 URL 的开头,它似乎做了一些称为 Route Globbing 的事情,这似乎只适用于路由段 http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments

如何让我在 RoR 中的应用程序始终以 www 开头。 ? -> www.foo.com 必须保持原样 -> foo.com 必须重定向到 www.foo.com

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 routing rails-routing


    【解决方案1】:

    最好在 nginx 或 apache 中执行(取决于您使用的是什么)。请参阅以下主题:

    对于 nginx Nginx no-www to www and www to no-www

    对于 Apache apache redirect from non www to www

    【讨论】:

    • 为什么在nginx或者apache中做比较好?
    • 因为进一步你可能会面临从 http 重定向到 https 的问题。因为 nginx 是用 C 编写的,专注于此类任务,所以它会比 Rails 快。
    【解决方案2】:

    您可以考虑使用机架中间件,例如 rack-wwwrack-rewrite。设置rack-www只需两步:

    # Add to Gemfile (and run `bundler`)
    gem 'rack-www'
    
    # configure middleware via `application.rb` (for all environments)
    # or the environment specific configuration file
    # `environments/<environment>.rb`
    config.middleware.use Rack::WWW, :www => true
    

    【讨论】:

      猜你喜欢
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 2017-10-26
      • 2010-12-21
      • 2014-07-12
      • 1970-01-01
      相关资源
      最近更新 更多