diff --git a/core/lib/Drupal/Core/Routing/RequestContext.php b/core/lib/Drupal/Core/Routing/RequestContext.php
index 2953251..c5222c4 100644
--- a/core/lib/Drupal/Core/Routing/RequestContext.php
+++ b/core/lib/Drupal/Core/Routing/RequestContext.php
@@ -27,6 +27,13 @@ class RequestContext extends SymfonyRequestContext {
   protected $completeBaseUrl;
 
   /**
+   * The URL to the main front controller, for example "/d8" or "/d8/index.php".
+   *
+   * @var string
+   */
+  protected $mainBaseUrl;
+
+  /**
    * Populates the context from the current request from the request stack.
    *
    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
@@ -47,6 +54,32 @@ public function fromRequest(Request $request) {
     if (isset($GLOBALS['base_url'])) {
       $this->setCompleteBaseUrl($GLOBALS['base_url']);
     }
+    if (isset($GLOBALS['base_path'])) {
+      $this->setMainBaseUrl('/' . ltrim($GLOBALS['base_path']));
+    }
+  }
+
+  /**
+   * Gets the base URL of /index.php, even if another front controller is used.
+   *
+   * @return string
+   */
+  public function getMainBaseUrl() {
+    return $this->mainBaseUrl;
+  }
+
+  /**
+   * Sts the main base URL for the request context.
+   *
+   * @param string $main_base_url
+   *   The main base URL.
+   *
+   * @return $this
+   */
+  public function setMainBaseUrl($main_base_url) {
+    $this->mainBaseUrl = $main_base_url;
+
+    return $this;
   }
 
   /**
@@ -64,9 +97,13 @@ public function getCompleteBaseUrl() {
    *
    * @param string $complete_base_url
    *   The complete base URL.
+   *
+   * @return $this
    */
   public function setCompleteBaseUrl($complete_base_url) {
     $this->completeBaseUrl = $complete_base_url;
+
+    return $this;
   }
 
 }
diff --git a/core/lib/Drupal/Core/Routing/UrlGenerator.php b/core/lib/Drupal/Core/Routing/UrlGenerator.php
index d990e40..2ac19ff 100644
--- a/core/lib/Drupal/Core/Routing/UrlGenerator.php
+++ b/core/lib/Drupal/Core/Routing/UrlGenerator.php
@@ -335,7 +335,7 @@ public function generateFromRoute($name, $parameters = array(), $options = array
       return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
     }
 
-    $base_url = $this->context->getBaseUrl();
+    $base_url = $this->context->getMainBaseUrl();
 
     $absolute = !empty($options['absolute']);
     if (!$absolute || !$host = $this->context->getHost()) {
