我的应用程序过去在foo.tld上运行,但现在它在bar.tld上运行。请求仍然会进入foo.tld,我想将它们重定向到bar.tld。
我如何在rails路由中做到这一点?
发布于 2012-06-07 01:32:24
这在Rails 3.2.3中有效
constraints(:host => /foo.tld/) do
match "/(*path)" => redirect {|params, req| "http://bar.tld/#{params[:path]}"}
end
这在Rails 4.0中有效
constraints(:host => /foo.tld/) do
match "/(*path)" => redirect {|params, req| "http://bar.tld/#{params[:path]}"}, via: [:get, :post]
end
发布于 2019-05-28 22:21:07
更现代的方法:
constraints(host: 'www.mydomain.com') do
get '/:param' => redirect('https://www.mynewurl.com/:param')
end
发布于 2020-06-23 02:39:18
与其他答案类似,这个答案对我也有效:
# config/routes.rb
constraints(host: "foo.com", format: "html") do
get ":any", to: redirect(host: "bar.com", path: "/%{any}"), any: /.*/
end
https://stackoverflow.com/questions/10918529
复制相似问题