Templates, Routes, and URL segments In EE, if there was a template /foo/bar.html, one could access the contents of that template by going to either example.com/foo/bar or example.com/foo/bar/more/segments/here. The additional URL segments didn't prevent EE from loading the most-accurately-matching template.

Moving over to Craft, bar is now an entry's slug. The section for that entry is properly set up with the "Entry URI Format" and "Template" pointing to foo/{slug} and foo/_entry.html, respectively. Is there a way to get the same "most-accurate" template-matching behavior in Craft? Perhaps with routes? I would want the {{ entry }} variable to be automatically available for either URL.
Yes and no. 
Yes it is possible to include as many additional segments to selected routes and it's even possible to achieve what you want but it might become resource heavy and you would need to handle the entire routing on your own.

Channels

If it's for a channel with a set URI format like foo/{slug} you can implement a route like

Event::on(
    UrlManager::class,
    UrlManager::EVENT_REGISTER_SITE_URL_RULES,
    function(RegisterUrlRulesEvent $event) {
        $event-&gt;rules[] = [
            'pattern'  =&gt; 'foo/&lt;uri:\S+&gt;/&lt;additionals:\S+&gt;',
            'route'    =&gt; 'your-module/your-controller/route',
            'defaults' =&gt; [
                'additionals' =&gt; ''
            ]
        ];
    }
}


So all your urls like foo/{slug} as well as foo/{slug}/bar/whatever  will be routed to your controller YourController::actionRoute with the parameters 

public function actionRoute(string $uri, string $additionals = null)
{
    $element = Craft::$app-&gt;getElements()-&gt;getElementByUri($uri);
    $template = (new Query())-&gt;select('template')
        -&gt;from('sections_sites')
        -&gt;where(['=', 'siteId', $element-&gt;site-&gt;id])
        -&gt;andWhere(['=', 'sectionId', $element-&gt;sectionId])
        -&gt;scalar();

    return $this-&gt;renderTemplate($template, ['entry' =&gt; $element]);
}


Note keep in mind this will only work if the uri format is rather simple like foo/{slug} it won't really work for things like foo/{author}/{category.id}/{slug}/{title} you'll need the rule mentioned in "structures" for it

Singles

If it's for singles you can do it dynamically 

try {
    $uris = (new Query())
        -&gt;select('uriFormat')
        -&gt;from('sections_sites')-
        -&gt;where(['=', 'siteId', Craft::$app-&gt;getSites()-&gt;getCurrentSite()-&gt;id])
        -&gt;andWhere(['=', 'sectionId', [array of your section ids])
        -&gt;column();
} catch (Exception $exception) {
    $uris = [];
}

if (empty($uris) === false) {
    $event-&gt;rules[] = [
        'pattern'  =&gt; '&lt;uri:(' . implode('|', $uris) . ')&gt;' . '/&lt;additional:\S+&gt;',
        'route'    =&gt; 'module/controller/action',
        'defaults' =&gt; [
            'additional' =&gt; ''
        ]
    ];
} 


Structures

For structures it's not that easy... You would need to recursively remove one segment and try to search for a matching route if there is one, use it, otherwise remove the next one.

Attention pseudocode

$path = get your full path
while($element === null){
    $path = $path - lastSegment;
    $element = Craft::$app-&gt;getElements()-&gt;getElementByUri($path);
}  
// attach the route `path` as you can see above


However I would not suggest the code above since it will search for a matching element for each segment for every request. I would highly recommend to include many conditions around it like Craft::$app-&gt;getRequest()-&gt;isSiteRequest() === true and $request-&gt;isAjaxRequest() === false and so on to reduce the amount of queries as much as possible