如何使用“redirect_to”删除请求的子域名?

时间:2013-01-03 00:13:11

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

如果我访问 http://walmart.example-site.com
它将我重定向到 http://walmart.example-site.com/communities/walmart

但我希望它重定向到 http://example-site.com/communities/walmart

我该如何解决?

我的代码是。

的routes.rb

constraints(:subdomain => /.+/) do 
    root :to => 'communities#subdomain_redirect' 
end 

communities_controller.rb

def subdomain_redirect
    @community = Community.find_by_community_name(request.subdomain)
    redirect_to @community
end

1 个答案:

答案 0 :(得分:1)

match "/stories/:name" => redirect {|params| "/posts/#{params[:name].pluralize}" }
match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }

这是你需要的吗?尝试参考secion 3.12 Redirection下的官方路由指南。