如果referrer domain = x,则重定向到Rails中的页面

时间:2013-07-10 20:20:10

标签: ruby-on-rails

我想将引荐来源网站检测到我的Rails网站,如果引荐来源与域名匹配,我希望将它们重定向到特定页面。

我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

class MyController < ApplicationController
  def my_action
    if request.referer =~ /a_domain_pattern/
      redirect_to a_specific_page_path
    end
  end
end

答案 1 :(得分:1)

您可以在ApplicationController中添加restrition:

class ApplicationController < ActionController::Base
  before_action :check_referer

  private

  def check_referer
    if request.referer =~/.hotdeals\.com./
      flash[:info] = "You are being redirected to a proper place"
      redirect_to url
    end
  end
end