Rails路由子域的重定向

cfe*_*nux 6 routes ruby-on-rails

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

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

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

问题在于子域名.我需要重定向例如:

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

如何使用routes.rb完成此操作?

Ste*_*ska 12

根据@ cfernandezlinux的惊人答案,Rails 4/Ruby 2语法中的相同内容如下:

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