Monday, September 15, 2008

Rails routing to handle crawler bots

To handle pesky bots triggering exception on non existing path, we can use:

# Catchall so we can gracefully handle badly formed requests
map.catch_all "*anything" , :controller => 'blog', :action => 'unknown_request'


Explanation about *anything:

Specifying *[string] as part of a rule like:
 map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?'

will glob all remaining parts of the route that were not recognized earlier. This idiom must appear at the end of the path. The globbed values are in params[:path] in this case.


Code snippet a little modified from Agile Web Development with Rails Second Edition. Explanation taken from the docs.

0 comments: