rails handle 404 with url redirect
I am looking to use rails to redi开发者_开发技巧rect links that I am sure are out there on the internet from an old domain of mine to a new one.
I would like to take address example.com/about ( about will not exist anymore)
and in my application_controller to take the 404, inspect the url and then redirect to
newexample.com/about
what's the best way to do this?
Add this to the end of your Routes file:
map.connect '*path', :controller => 'some_controller', :action => 'some_action'
This will catch any 404. Within the controller and action that will handle this route, use params[:path]
to examine the url. Then you can redirect_to
based on whatever is contained in params[:path]
.
You need to use rescue_from. In your ApplicationController
, add something like:
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
精彩评论