使用url重定向的rails handle 404

我希望使用rails来重定向我确信在互联网上从我的旧域到新域的链接。

我想采取地址example.com/about(关于将不再存在)

并在我的application_controller中获取404,检查URL然后重定向到

newexample.com/about

什么是最好的方法呢?

将其添加到Routes文件的末尾:

map.connect '*path', :controller => 'some_controller', :action => 'some_action' 

这将捕获任何404.在将处理此路由的控制器和操作中,使用params[:path]检查URL。 然后你可以根据params[:path]包含的内容redirect_to

你需要使用rescue_from 。 在ApplicationController ,添加如下内容:

 rescue_from ActionController::RoutingError, :with => :redirect_missing rescue_from ActionController::UnknownController, :with => :redirect_missing rescue_from ActionController::UnknownAction, :with => :redirect_missing def redirect_missing redirect_to "http://newexample.com/about" end