每次重定向/在rails上使用特定url时记录或通知



我有一个/view/id格式的旧链接,已重定向到view/category/id

每次重定向发生时,我都想登录或收到通知,看看是否可以在使用旧链接的地方捕获一些剩余代码。

有什么办法我能做到这一点吗?

您可以添加一个自定义ActiveSupport::Subscriber,如下所示

# app/subscribers/specify_redirected_subscriber
class SpecifyRedirectedSubscriber < ActiveSupport::Subscriber
attach_to :action_controller
SPECIFY_REDIRECTED = [/your-domain/view/category/d/] # view/category/id
def redirect_to(event)
redirected_link = event.payload[:location]

matched = SPECIFY_REDIRECTED.filter { |specify_link| 
specify_link.match(redirected_link) 
}

unless matched.blank?
# log what you want
Rails.logger.info { "Specify Redirected To #{redirected_link}" }
end
end
end

然后您可以在应用程序的底部添加此订阅者

class ApplicationController < ActionController::Base
# ...
end
SpecifyRedirectedSubscriber

最新更新