简体   繁体   English

如何将子域重定向到 rails 路由中的新 url

[英]How to redirect subdomain to a new url in rails routes

When I type mcca.example.com in url, it redirects me to ncca.example.com .当我在 url 中输入mcca.example.com时,它会将我重定向到ncca.example.com

How do I make this redirect in rails routes?如何在 rails 路线中进行此重定向?

constraints subdomain: "mcca" do
   get "/", to: redirect  { |params| root(subdomain: "ncca") }
end

This is what i have tried with.这是我尝试过的。

With thanks to this question and answer感谢这个问题和答案

In Rails 4 and 5:在 Rails 4 和 5 中:

get '/mcca', to: redirect('/ncca') # or something like that based on your requirements. get '/mcca', to: redirect('/ncca') # 或类似的内容,根据您的要求。

In Rails 3:在 Rails 3 中:

You can redirect inside the routes.rb file.您可以在 routes.rb 文件中重定向。 match "/mcca" => redirect(" http://example.com/ncca ")匹配 "/mcca" => 重定向(" http://example.com/ncca ")

You can use route constraints for handling subdomains.您可以使用路由约束来处理子域。 Have a look on the official Rails guides here 在此处查看官方 Rails 指南

You may add this at the beginning of your routes.rb file:您可以在routes.rb文件的开头添加:

get '/', to: 'welcome#index', constraints: { subdomain: 'mcca' }
get '/', to: 'ncca#index', constraints: {subdomain: 'ncca' }

then in a WelcomeController , at the end of an index action you can perform a substitution to the request url with:然后在WelcomeController 中,在索引操作结束时,您可以使用以下命令替换请求 url:

def index
  ...
  redirect_to request.url.sub('mcca', 'ncca')
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM