簡體   English   中英

如何使用基於正則表達式的約束定義Ruby on Rails路由來忽略列入黑名單的單詞

[英]How do I define Ruby on Rails route using regex based constraints to ignore blacklisted word

我設置了一個虛擬服務器,該虛擬服務器指向Rails應用程序上的頁面。 我在定義當我的GET請求到達mysite.com/BLACKLIST_WORD時忽略BLACKLIST_WORD的路由時遇到問題。 不幸的是,這是我的工作,它不起作用。

  get '*ignore_me' => 'pages#unknown_url' , :constraints => { :ignore_me => /^/(?!BLACKLIST_WORD/).+/ }

我的unknown_url方法對請求進行了其他處理,但我希望將其列入黑名單。 請幫助,加油!

無需檢查,但我想您必須轉義/

所以應該

/^\/(?!BLACKLIST_WORD\/).+/

也許第二/甚至可選? \\/?

這就是我所做的。

get '*path', to: 'pages#unknown_url', constraints: BlacklistConstraint.new

class BlacklistConstraint
  def initialize
    # @ips = Blacklist.retrieve_ips
    @word_list = [
      "/BLACKLIST_WORD"
    ]
  end

  def matches?(request)
    # @ips.include?(request.remote_ip)
    puts request.path
    if @word_list.include?(request.path)
      return false
    else
      return true
    end
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM