diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php
index fb2108d46233ead4806656226c1c28531c155269..b477c8a855e1410df61f4c5e058691d85e9a83ab 100644
--- a/lib/SimpleSAML/Configuration.php
+++ b/lib/SimpleSAML/Configuration.php
@@ -454,9 +454,13 @@ class SimpleSAML_Configuration
             return \SimpleSAML\Utils\HTTP::getFirstPathElement(false).$matches[1];
         }
 
-        if (preg_match('#^https?://[^/]*/(.*)$#', $baseURL, $matches)) {
+        if (preg_match('#^https?://[^/]*(?:/(.+/?)?)?$#', $baseURL, $matches)) {
             // we have a full url, we need to strip the path
-            return $matches[1];
+            if (!array_key_exists(1, $matches)) {
+                // root directory specified with an absolute URL
+                return '';
+            }
+            return rtrim($matches[1], '/')."/";
         } elseif ($baseURL === '' || $baseURL === '/') {
             // Root directory of site
             return '';
diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php
index 2251eee59a0d120b51a6bb661cead6cb7f390b1b..d802370dcb4561f0bdd81ea449066f0787b31b8d 100644
--- a/lib/SimpleSAML/Utils/HTTP.php
+++ b/lib/SimpleSAML/Utils/HTTP.php
@@ -566,9 +566,9 @@ class HTTP
         $globalConfig = \SimpleSAML_Configuration::getInstance();
         $baseURL = $globalConfig->getString('baseurlpath', 'simplesaml/');
 
-        if (preg_match('#^https?://.*/$#D', $baseURL, $matches)) {
+        if (preg_match('#^https?://.*/?$#D', $baseURL, $matches)) {
             // full URL in baseurlpath, override local server values
-            return $baseURL;
+            return rtrim($baseURL, '/').'/';
         } elseif (
             (preg_match('#^/?([^/]?.*/)$#D', $baseURL, $matches)) ||
             (preg_match('#^\*(.*)/$#D', $baseURL, $matches)) ||