当我在 url 中键入mcca.example.com
时,它会将我重定向到ncca.example.com
.
如何在铁路路线中进行此重定向?
constraints subdomain: "mcca" do
get "/", to: redirect { |params| root(subdomain: "ncca") }
end
这是我尝试过的。
感谢这个问题和答案
在轨道 4 和 5 中:
获取"/MCCA",到: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