代码之家  ›  专栏  ›  技术社区  ›  maček

如何在没有www的情况下使用Rails 3/Rack重定向?

  •  37
  • maček  · 技术社区  · 14 年前

    我知道有很多问题可以回答这个问题。我很熟悉 .htaccess nginx.conf 方法,但我无法访问Heroku上的这种传统配置方法。

    Simone Carletti 我在用Rails 3 这是不兼容的:

    Redirect non-www requests to www URLs in Ruby on Rails

    请注意:

    我不是在找一个简单的 before_filter

    目标

    redirect                to                  status
    ----------------------------------------------------
    www.foo.com             foo.com             301
    www.foo.com/whatever    foo.com/whatever    301
    

    只有 主机匹配 /^www\./

    9 回复  |  直到 5 年前
        1
  •  51
  •   Peter Mortensen icecrime    5 年前

    Ruby on Rails 4版

    # config/routes.rb
    
    constraints subdomain: 'www' do
      get ':any', to: redirect(subdomain: nil, path: '/%{any}'), any: /.*/
    end
    

    相比之下,将www.添加到任何尚未拥有它的URL的开头可以通过以下方式实现:

    # config/routes.rb
    
    constraints subdomain: false do
      get ':any', to: redirect(subdomain: 'www', path: '/%{any}'), any: /.*/
    end
    
        2
  •  13
  •   Sean Schofield    14 年前

    如果您使用的是Rails 3,那么会有更好的方法。好好利用这条路线。

    Foo::Application.routes.draw do
      constraints(:host => /^example.com/) do
        root :to => redirect("http://www.example.com")
        match '/*path', :to => redirect {|params| "http://www.example.com/#{params[:path]}"}
      end
    end
    
        3
  •  12
  •   Duke    14 年前

    我真的很喜欢用Rails路由器来做这些事情。以前的答案是好的,但我想要一些通用的东西,我可以使用任何网址,以“www”开头。

    constraints(:host => /^www\./) do
      match "(*x)" => redirect { |params, request|
        URI.parse(request.url).tap {|url| url.host.sub!('www.', '') }.to_s
      }
    end
    
        4
  •  7
  •   Ilya Sabanin    14 年前

    看看这个中间件吧,它应该能做您想要的事情:

    http://github.com/iSabanin/www_ditcher

    如果对你有用,告诉我。

        5
  •  7
  •   Will Koehler    13 年前

    杜克解决方案的单线版本。只需添加到routes.rb的顶部

    match '(*any)' => redirect { |p, req| req.url.sub('www.', '') }, :constraints => { :host => /^www\./ }
    
        6
  •  6
  •   Vikrant Chaudhary    8 年前

    在轨道3中

    #config/routes.rb
    Example::Application.routes.draw do
      constraints(:host => "www.example.net") do
        match "(*x)" => redirect { |params, request|
          URI.parse(request.url).tap { |x| x.host = "example.net" }.to_s
        }
      end
      # .... 
      # .. more routes ..
      # ....
    end
    
        7
  •  3
  •   scarver2    13 年前

    如果要从顶级域(TLD)重定向到www子域,请使用以下代码:

    constraints :subdomain => '' do
      match '(*any)' => redirect { |p, req| req.url.sub('//', '//www.') }
    end
    

        8
  •  3
  •   Jessie Dedecker    12 年前

    对于Rails 4,上面的解决方案必须附加动词结构,例如。 via: [:get, :post] . 杜克的解决方案是:

      constraints(:host => /^www\./) do
        match "(*x)" => redirect { |params, request|
          URI.parse(request.url).tap {|url| url.host.sub!('www.', '') }.to_s
        }, via: [:get, :post]
      end
    
        9
  •  2
  •   Graham Ashton    12 年前

    上面的方法没有什么问题,但是也有一些gem提供了机架中间件来实现这一点。

    我喜欢他们将这种行为与应用程序本身分开的方式,但这两种方式都不是一个特别有力的论据。在使用Sinatra时,我也使用中间件来实现这一点,所以我更喜欢使用一种技术,可以在基于Rails和/或Sinatra构建的应用程序上使用(我经常运行嵌入在Rails中的Nesta)。

    不管怎样,给你:

    第一个更简单(我一直在使用的那个),而第二个提供了更多的功能(我还不需要,但很感激)。