diff --git a/lib/SimpleSAML/Auth/Simple.php b/lib/SimpleSAML/Auth/Simple.php
index 769c0ab55230ed6eadf7637aec97ae10ef03e1ef..2bfa424ec821ddfec48306b82f69867b0c8b4f67 100644
--- a/lib/SimpleSAML/Auth/Simple.php
+++ b/lib/SimpleSAML/Auth/Simple.php
@@ -377,7 +377,7 @@ class Simple
         $scheme = parse_url($url, PHP_URL_SCHEME);
         $host = parse_url($url, PHP_URL_HOST) ? : Utils\HTTP::getSelfHost();
         $port = parse_url($url, PHP_URL_PORT) ? : (
-            $scheme ? '' : trim(Utils\HTTP::getServerPort(), ':')
+            $scheme ? '' : ltrim(Utils\HTTP::getServerPort(), ':')
         );
         $scheme = $scheme ? : (Utils\HTTP::getServerHTTPS() ? 'https' : 'http');
         $path = parse_url($url, PHP_URL_PATH) ? : '/';
@@ -394,7 +394,7 @@ class Simple
             return $scheme.'://'.$host.$port.$path.($query ? '?'.$query : '').($fragment ? '#'.$fragment : '');
         }
 
-        $base = trim($this->app_config->getString(
+        $base = rtrim($this->app_config->getString(
             'baseURL',
             $scheme.'://'.$host.$port
         ), '/');