diff --git a/core/lib/Drupal/Core/Routing/RouteCompiler.php b/core/lib/Drupal/Core/Routing/RouteCompiler.php
index 9c430f6..aa61f64 100644
--- a/core/lib/Drupal/Core/Routing/RouteCompiler.php
+++ b/core/lib/Drupal/Core/Routing/RouteCompiler.php
@@ -17,6 +17,11 @@ class RouteCompiler extends SymfonyRouteCompiler implements RouteCompilerInterfa
   const REGEX_DELIMITER = '#';
 
   /**
+   * Flag the path as having unlimited parts.
+   */
+  const UNLIMITED_PARTS = -1;
+
+  /**
    * Compiles the current route instance.
    *
    * Because so much of the parent class is private, we need to call the parent
@@ -42,6 +47,14 @@ public static function compile(Route $route) {
     // allows the RouteProvider to filter candidate routes more efficiently.
     $num_parts = count(explode('/', trim($route->getPath(), '/')));
 
+    $unlimited_requirements = array_filter($route->getRequirements(), function ($it) {
+      return $it === '.*' || $it === '.+';
+    });
+
+    if (count($unlimited_requirements) > 0) {
+      $num_parts = static::UNLIMITED_PARTS;
+    }
+
     return new CompiledRoute(
       $fit,
       $pattern_outline,
diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php
index 95cbd67..91248fd 100644
--- a/core/lib/Drupal/Core/Routing/RouteProvider.php
+++ b/core/lib/Drupal/Core/Routing/RouteProvider.php
@@ -358,9 +358,10 @@ protected function getRoutesByPath($path) {
     // trailing wildcard parts as long as the pattern matches, since we
     // dump the route pattern without those optional parts.
     try {
-      $routes = $this->connection->query("SELECT name, route, fit FROM {" . $this->connection->escapeTable($this->tableName) . "} WHERE pattern_outline IN ( :patterns[] ) AND number_parts >= :count_parts", [
+      $routes = $this->connection->query("SELECT name, route, fit FROM {" . $this->connection->escapeTable($this->tableName) . "} WHERE pattern_outline IN ( :patterns[] ) AND (number_parts >= :count_parts OR number_parts = :unlimited_parts)", [
         ':patterns[]' => $ancestors,
         ':count_parts' => count($parts),
+        ':unlimited_parts' => RouteCompiler::UNLIMITED_PARTS,
       ])
         ->fetchAll(\PDO::FETCH_ASSOC);
     }
