5

I have a set of routes set up like the following:

routes: {
  '!/home': 'home',
  '!/home/:page': 'home'
}

What I'm wondering is, how do I configure a route such that if the user requests an unknown page I can easily redirect to a static 404.html page?

Darrell Brogdon
  • 6,843
  • 9
  • 47
  • 62

1 Answers1

10

In your Router (the first two routes are for example):

routes: {
  "path/": "objectList",
  "path/:id": "objectItem",
  ":whatever": "notFound"
}

The last is the most general route possible, and will pick up everything not recognized by more specific routes.

Elf Sternberg
  • 16,129
  • 6
  • 60
  • 68
  • ":whatever" will only match up to the first /. Use "*whatever" to match any number of URL components – Mads Mobæk Feb 02 '12 at 16:14
  • 3
    *whatever ends up matching all my routes - I want it to be hit only if none of the other urls matched – Shreyas May 28 '12 at 10:49
  • What if you're using multiple instances of Backbone.Router? This could pose a problem, as the default route (in this case, `:whatever`) would match any route, thereby preventing a different router instance from attempting to route the request. – bibs Apr 29 '13 at 15:15
  • Thanks! @ElfSternberg Are these references available anywhere in the backbone documentation? – theunexpected1 Oct 13 '14 at 13:37
  • Not that I know of. That's why there's the annotated source code. It's better documentation than the documentation. – Elf Sternberg Oct 13 '14 at 18:28
  • I found something useful, if it helps: http://mrbool.com/backbone-js-router/28001 – theunexpected1 Oct 14 '14 at 06:28