【问题标题】:Rails routes redirection for subdomainsRails 路由重定向子域
【发布时间】:2012-12-05 01:27:56
【问题描述】:

我们无法更改服务器配置文件,因此我们需要在 rails 级别进行重定向。

我对到外部站点的路径重定向没有问题,例如:

match "/meow" => redirect("http://meow.com/")

问题在于子域。例如,我需要重定向:

http://my.example.com => http://example.com

如何使用 routes.rb 来做到这一点?

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    根据@cfernandezlinux 的amazing answer,这在Rails 4/Ruby 2 语法中是一样的:

    constraints subdomain: "meow" do   
      get "/" => redirect { |params| "http://www.externalurl.com" }
    end
    
    • match 在 Rails 4.0 中不再允许在 routes.rb 中使用。您必须明确使用getpost 等。
    • hashrocket 语法 (=>) 用于旧 Ruby,现在在 Ruby 2.0 中我们使用 param: 'value' 语法

    【讨论】:

      【解决方案2】:

      我最终做了这样的事情:

      constraints :subdomain => "meow" do   
        match "/" => redirect { |params| "http://www.externalurl.com" }
      end
      

      【讨论】:

        【解决方案3】:

        如果您不想对 URL 进行硬编码(例如,您可以在本地测试/使用它),您可以这样做:

          constraints subdomain: 'subdomain_from' do
            get '/' => redirect(subdomain: :new_subdomain, path: '')
          end
        

        所以现在subdomain_from.google.com 将重定向到new_subdomain.google.com

        【讨论】:

          【解决方案4】:

          【讨论】:

          • 谢谢,我实际上找到了一个包含相同信息的论坛帖子。我最终这样做了:
          猜你喜欢
          • 1970-01-01
          • 2018-10-22
          • 2021-11-17
          • 2020-04-27
          • 2014-04-27
          • 1970-01-01
          • 1970-01-01
          • 2013-12-04
          • 1970-01-01
          相关资源
          最近更新 更多