To do this well, it's not really something that can be covered within a simple forum reply, and there are also numerous ways of implementing it. The basics however are simple:
Break the url into parts, and match those parts against a series of regular expressions. If those parts regular expressions match, check to see if we can turn that match into an object and method within said object. If we can, execute them, otherwise, rinse and repeat.
As an example, I'll show you how I have implemented it. It basically all comes down to the order in which your routes are matched. If you look at my framework's (Proem - yes I'm going to use it as an example) default routes (here) you should be able to see how this can be implemented (Each of these *tokens* starting with : are replaced internally with more complex regular expressions). When it comes time to attempt to dispatch a request to a controller, all of these route rules are looped through and checked against the current request url. When one matches, the dispatcher will check to see if it can actually instantiate a controller and call the *action* method.
In Proem this happens in two stages. Firstly we loop through all matching routes (here) and when a match is found a route.match event is triggered which in turn tests to see if the object can be dispatched (here). The actual check happens within the standard dispatcher here.