There are several possibilities. Here is one. You can add this to the bottom of your .rb routes:
match ':not_found' => 'my_controller#index', :constraints => { :not_found => /.*/ }
which sets the entire route to force the MyController index action to handle any missing paths; he can detect them by looking at params[:not_found] and doing whatever he wants, for example, redirects to root_path ( redirect_to root_url ), redirects somewhere strategically based on a bad path, something special, exploring the referrer / referent for tips about source, etc.
Requires option :constraints ; otherwise, the not_found parameter not_found not contain special characters, such as slashes and not_found .
Put this at the bottom of your routes, because obviously it will fit all, and you want your other routes to crack along the way first.
If you only want to redirect, nothing more, you can do it instead (again, at the bottom ):
match ':not_found' => redirect('/'), :constraints => { :not_found => /.*/ }
Rob davis
source share