我知道在 ngnix 中应该更好,但我必须在 Rails (routes.rb) 中进行
我的旧网址是:
http://国家 .example.com/whatever/whatever
新网址
http://example.com/ 国家/随便/随便
最佳答案
原答案
有约束(仅当子域是 country.example.com 时适用)
constraints :subdomain => "country" do
get "programs/:id", to: redirect { |params, req| "http://#{req.domain}/#{req.subdomain}/programs/#{params[:id]}" }
end
无限制(适用于任何子域,chicken.example.com,waffles.example.com)
get 'programs/:id', to: redirect { |params, req| "#{req.subdomain}/programs/#{params[:id]}" }
编辑以匹配所有参数
指定的路由通配here应该是。这应该有效:
constraints :subdomain => "country" do
get "*all", to: redirect { |params, req| "http://#{req.domain}/#{req.subdomain}/#{params[:all]}" }
end
基本上它只是捕获 params[:all] 中的所有参数并使用它来转发它们。请务必将其包装在子域约束中,因为它可以捕获您所有的 get 请求,具体取决于它在您的
routes.rb
中的位置。文件
关于ruby-on-rails - 将子域重定向到 Rails 中的子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926242/