【问题标题】:How to redirect subdomain to a new url in rails routes如何将子域重定向到rails路由中的新url
【发布时间】:2020-04-27 19:16:11
【问题描述】:

当我在 url 中输入 mcca.example.com 时,它会将我重定向到 ncca.example.com

如何在 Rails 路线中进行此重定向?

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

这是我尝试过的。

【问题讨论】:

  • 您是否尝试过更新您的域的 DNS 设置来完成此操作?
  • 我正在尝试在路由中进行重定向。我没有更改 DNS 设置

标签: ruby-on-rails


【解决方案1】:

您可以使用路由约束来处理子域。 看看on the official Rails guides here

您可以在 routes.rb 文件的开头添加此内容:

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

然后在 WelcomeController 中,在 index 操作结束时,您可以使用以下命令替换请求 url:

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

【讨论】:

    【解决方案2】:

    感谢this question and answer

    在 Rails 4 和 5 中:

    get '/mcca', to: redirect('/ncca') # 或类似的东西,根据你的要求。

    在 Rails 3 中:

    您可以在 routes.rb 文件中进行重定向。 匹配 "/mcca" => 重定向("http://example.com/ncca")

    【讨论】:

      猜你喜欢
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 2015-02-16
      • 2021-11-17
      • 2018-10-22
      相关资源
      最近更新 更多