Symfony default, catch-all route for non-existent URLs

Symfony’s routing system is pretty easy to understand, however, what happens when you need a default, catch-all route that doesn’t match any other routes?  Instead of sending a customer to a 404, you can redirect them to a default path, in this case, it’s the root path “/” (defined in the line “path: /” below) but it could be another path if you’d like.  Remember that the order of the routes are important and take precedence over ones Symfony encounters later down the routing directives list.

defaultCatchAll:
    path:     /{path}
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /
        permanent: true
    requirements:
        path: ".+"

Visit Symfony’s documentation for more details:
http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html
http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *