diff --git a/core/lib/Drupal/Core/EventSubscriber/LegacyControllerSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/LegacyControllerSubscriber.php
index 7009eff9154ed3128de8e40cbbe9491d8f5c0894..f88fa0e71e565669bd4cfe7dcc0dceea4bf4a4d4 100644
--- a/core/lib/Drupal/Core/EventSubscriber/LegacyControllerSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/LegacyControllerSubscriber.php
@@ -42,12 +42,6 @@ public function onKernelControllerLegacy(FilterControllerEvent $event) {
 
     // This BC logic applies only to functions. Otherwise, skip it.
     if (is_string($controller) && function_exists($controller)) {
-      // Flag this as a legacy request.  We need to use this for subrequest
-      // handling so that we can treat older page callbacks and new routes
-      // differently.
-      // @todo Remove this line as soon as possible.
-      $request->attributes->set('_legacy', TRUE);
-
       $new_controller = function() use ($router_item) {
         return call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);
       };
diff --git a/core/lib/Drupal/Core/LegacyUrlMatcher.php b/core/lib/Drupal/Core/LegacyUrlMatcher.php
index c1175abdc4c947392369c146a6a156b4b60b9c0b..7098b412ee6685be0aa821ce83fa7f84cd98b5b9 100644
--- a/core/lib/Drupal/Core/LegacyUrlMatcher.php
+++ b/core/lib/Drupal/Core/LegacyUrlMatcher.php
@@ -111,6 +111,12 @@ public function matchRequest(Request $request) {
         require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
       }
 
+      // Flag this as a legacy request.  We need to use this for subrequest
+      // handling so that we can treat older page callbacks and new routes
+      // differently.
+      // @todo Remove this line as soon as possible.
+      $ret['_legacy'] = TRUE;
+
       return $ret;
     }