[英]How to redirect subdomain to a new url in rails routes
当我在 url 中输入mcca.example.com
时,它会将我重定向到ncca.example.com
。
如何在 rails 路线中进行此重定向?
constraints subdomain: "mcca" do
get "/", to: redirect { |params| root(subdomain: "ncca") }
end
这是我尝试过的。
感谢这个问题和答案
在 Rails 4 和 5 中:
get '/mcca', to: redirect('/ncca') # 或类似的内容,根据您的要求。
在 Rails 3 中:
您可以在 routes.rb 文件中重定向。 匹配 "/mcca" => 重定向(" http://example.com/ncca ")
您可以使用路由约束来处理子域。 在此处查看官方 Rails 指南
您可以在routes.rb文件的开头添加:
get '/', to: 'welcome#index', constraints: { subdomain: 'mcca' }
get '/', to: 'ncca#index', constraints: {subdomain: 'ncca' }
然后在WelcomeController 中,在索引操作结束时,您可以使用以下命令替换请求 url:
def index
...
redirect_to request.url.sub('mcca', 'ncca')
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.