Catching all non-matching URLs on Rails

One of the early things that I needed to for a web application, was to try to map all unhandled or malformed URLs to be sent to Rails. It’s important to do that especially when it’s too easy to make a mistake typing on a URL, also because that instead of the typical 404 message people normally see, Rails gives you an unsightly routing error, with a “no route to match” message:


In order to change that and assign a controller to any unhandled URLs, append the following line as the last statement of your config/routes.rb:

map.connect '*anything', :controller => 'controller', :action => 'action'

Replace controller and action with the desired controller and action respectively. This will allow all unhandled URLs to be managed by the controller of your choosing.