【问题标题】:Is there a way in rails 3 to redirect any Internet Explorer user to specific page?rails 3 中有没有办法将任何 Internet Explorer 用户重定向到特定页面?
【发布时间】:2011-05-11 17:55:04
【问题描述】:

我想将所有 IE 用户重定向到一个特定页面,告诉他们我们不支持 IE,他们应该尝试其他浏览器。如何使用 rails 3 路线做到这一点,同时仍允许公共用户查看网站的公共区域?

我的路线是这样的......

MyApp::Application.routes.draw do
  constraints(NoSubdomain) do
    root :to => 'public#index'
  end

  constraints(Subdomain) do
    root :to => 'internal#index'
  end
end

我知道有匹配 user_agent 的功能,但我无法将所有尝试转到网站内部部分 (account.myapp.com) 的用户重定向到 '/ie'页。有什么想法吗?

MyApp::Application.routes.draw do
  constraints(NoSubdomain) do
    root :to => 'public#index'
  end

  constraints :user_agent => /MSIE/ do
    match "*" => redirect('/ie')
  end

  constraints(Subdomain) do
    root :to => 'internal#index'
  end
end

【问题讨论】:

  • 真正让事情在 IE 中运行怎么样?我可以理解不支持 IE6 及以下版本,但是来吧......无论如何,浏览器检测是通过用户代理完成的,在 IE 的情况下,它可以通过子字符串 MSIE 来识别。
  • +1 拒绝 IE 用户!!挂他们!!放火烧他们!!

标签: internet-explorer ruby-on-rails-3 redirect routes


【解决方案1】:

这里似乎有两个问题。

首先,您在匹配所有内容时调用了重定向。因此,用户请求 account.myapp.com/ 并且匹配任何内容,因此他们被重定向到 account.myapp.com/ie,但也匹配任何内容,因此他们被重定向到 account.myapp.com/ie 并且他们被重定向再次。它变成了一个无限循环,但您可能没有看到这一点,因为 Rails 由于第二件事甚至无法匹配它。

其次,Rails 要求您在其中输入名称。您可以使用“url”或您喜欢的任何内容(路径、页面等)。这样一来,Rails 就可以在 params 中将请求的路径分配给该名称——这样无论路径是什么,您都可以得到 params[:url]。我有点惊讶它不会让你只使用“”,但它不会。

这对我有用:

constraints :user_agent => /Firefox/ do
  match "*url", :to => "ie#index"
end

我在 Mac 上,没有 IE 进行测试,但您可以轻松地将 IE 分入其中。

【讨论】:

    猜你喜欢
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2012-04-01
    相关资源
    最近更新 更多