Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails handle 404 with url redirect

I am looking to use rails to redirect 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?

like image 881
mattwallace Avatar asked Mar 08 '11 14:03

mattwallace


1 Answers

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].

like image 89
Charles Caldwell Avatar answered Oct 01 '22 04:10

Charles Caldwell