Route for any image?


I want to create a route for any image. This is in order to create an
report explaining what the missing image is. I could create a route for
each directory that currently has images in it but wanted something more
general like:

map.connect “/**/*.gif”, :controller => ‘image_handler’, :action =>

Which doesn’t work. Anyone know more about the inner workings?



Here’s one way:

In your config/routes.rb file:
map.connect “*anything”, :controller => “unknown”, :action =>

In your app/controllers/unknown_controller.rb file:

Unknown URLs get routed to this action.

def unknown_url
missing_image =

  if missing_image
     img_name = missing_image[1]
     logger.error("Request for nonexistent image #{img_name} at

URI #{@request.request_uri}…")
# Send your email here…




You can only use * once. It catches everything and stores it into a
variable of your choosing. (e.g. map.connect ‘*path’ will give you the
contents of the URL in params[:path])


map.connect ‘some/*path’, :controller => ‘pages’, :action =>

But… I don’t think routes are the best way to achieve your goal, if
your goal is to scan your site for broken images. (What WOULD be a
good way I dunno…) (I doubt you can even do what you want with

  • Rabbit