diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php
index 3903e726e5dbe32bc170d8b561ef77ce9a5319b3..50c3209af3a8055878530ac0237552f2337cbbe7 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php
@@ -207,11 +207,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandler
         }
 
         // then we look for the hostname
-        $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org
-        if (strpos($currenthost, ":") !== false) {
-            $currenthostdecomposed = explode(":", $currenthost);
-            $currenthost = $currenthostdecomposed[0];
-        }
+        $currenthost = \SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); // sp.example.org
 
         foreach ($this->sources as $source) {
             $index = $source->getEntityIdFromHostPath($currenthost, $set, $type);
diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php
index 3f863222754b0545ad5f973cc86acd9490eff0dc..80bade1c933eebfa647eea52986c8030c4327089 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php
@@ -134,9 +134,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile extends SimpleSAML_Meta
         } elseif ($set === 'shib13-idp-hosted') {
             return $baseurl.'shib13/idp/metadata.php';
         } elseif ($set === 'wsfed-sp-hosted') {
-            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
+            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort();
         } elseif ($set === 'adfs-idp-hosted') {
-            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp';
+            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort().':idp';
         } else {
             throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']');
         }
diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php
index e8981169d4984b18b9fb0d0ebd8e8b766b1bd397..7202a73f930fc90763db395d4c6e24ebaf156d3b 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php
@@ -151,9 +151,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_
         } elseif ($set === 'shib13-sp-hosted') {
             return $baseurl.'shib13/sp/metadata.php';
         } elseif ($set === 'wsfed-sp-hosted') {
-            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
+            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort();
         } elseif ($set === 'adfs-idp-hosted') {
-            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp';
+            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort().':idp';
         } else {
             throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']');
         }
diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php
index bc7e896eaa68eb426ee64486942282a98a04a763..d442bf7829a5471ab289cfc96841bd3c00e56eb2 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php
@@ -198,11 +198,7 @@ abstract class SimpleSAML_Metadata_MetaDataStorageSource
         $metadataSet = $this->getMetadataSet($set);
 
         // check for hostname
-        $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org
-        if (strpos($currenthost, ":") !== false) {
-            $currenthostdecomposed = explode(":", $currenthost);
-            $currenthost = $currenthostdecomposed[0];
-        }
+        $currenthost = \SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); // sp.example.org
 
         foreach ($metadataSet as $index => $entry) {
             if ($index === $entityId) {
diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php
index 5f791def704e11d185b31e51df5bab82dfd2192c..c25bc6d57b8d03a91678403ce4c535e7079ab3b1 100644
--- a/lib/SimpleSAML/Utils/HTTP.php
+++ b/lib/SimpleSAML/Utils/HTTP.php
@@ -336,7 +336,7 @@ class HTTP
 
 
     /**
-     * Helper function to retrieve a file or URL with proxy support, also 
+     * Helper function to retrieve a file or URL with proxy support, also
      * supporting proxy basic authorization..
      *
      * An exception will be thrown if we are unable to retrieve the data.
@@ -605,11 +605,25 @@ class HTTP
         $url = self::getBaseURL();
 
         $start = strpos($url, '://') + 3;
-        $length = strcspn($url, '/:', $start);
+        $length = strcspn($url, '/', $start);
 
         return substr($url, $start, $length);
     }
 
+    /**
+     * Retrieve our own host.
+     *
+     * @return string The current host without port specification.
+     */
+    public static function getSelfHostWithoutPort()
+    {
+        $url = self::getBaseURL();
+
+        $start = strpos($url, '://') + 3;
+        $length = strcspn($url, '/:', $start);
+
+        return substr($url, $start, $length);
+    }
 
     /**
      * Retrieve our own host together with the URL path. Please note this function will return the base URL for the
diff --git a/tests/lib/SimpleSAML/Utils/HTTPTest.php b/tests/lib/SimpleSAML/Utils/HTTPTest.php
new file mode 100644
index 0000000000000000000000000000000000000000..b4e9845a72cfdd0133e655fa448ef431ebd2756f
--- /dev/null
+++ b/tests/lib/SimpleSAML/Utils/HTTPTest.php
@@ -0,0 +1,35 @@
+<?php
+namespace SimpleSAML\Test\Utils;
+
+use SimpleSAML\Utils\HTTP;
+
+class HTTPTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * Test HTTP::getSelfHost with and without custom port
+     */
+    public function testGetSelfHost()
+    {
+        \SimpleSAML_Configuration::loadFromArray(array(
+            'baseurlpath' => '',
+        ), '[ARRAY]', 'simplesaml');
+        $_SERVER['SERVER_PORT'] = '80';
+        $this->assertEquals('localhost', HTTP::getSelfHost());
+        $_SERVER['SERVER_PORT'] = '3030';
+        $this->assertEquals('localhost:3030', HTTP::getSelfHost());
+    }
+
+    /**
+     * Test HTTP::getSelfHostWithoutPort
+     */
+    public function testGetSelfHostWithoutPort()
+    {
+        \SimpleSAML_Configuration::loadFromArray(array(
+            'baseurlpath' => '',
+        ), '[ARRAY]', 'simplesaml');
+        $_SERVER['SERVER_PORT'] = '80';
+        $this->assertEquals('localhost', HTTP::getSelfHostWithoutPort());
+        $_SERVER['SERVER_PORT'] = '3030';
+        $this->assertEquals('localhost', HTTP::getSelfHostWithoutPort());
+    }
+}
diff --git a/www/admin/hostnames.php b/www/admin/hostnames.php
index 4baec753b180280140b7c6d7e386bc68bb6587fd..e77d99edcc751cfc58966ff637ff577cfb01de11 100644
--- a/www/admin/hostnames.php
+++ b/www/admin/hostnames.php
@@ -17,7 +17,7 @@ $attributes['SERVER_PROTOCOL'] = array($_SERVER['SERVER_PROTOCOL']);
 $attributes['SERVER_PORT'] = array($_SERVER['SERVER_PORT']);
 
 $attributes['Utilities_getBaseURL()'] = array(\SimpleSAML\Utils\HTTP::getBaseURL());
-$attributes['Utilities_getSelfHost()'] = array(\SimpleSAML\Utils\HTTP::getSelfHost());
+$attributes['Utilities_getSelfHost()'] = array(\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort());
 $attributes['Utilities_selfURLhost()'] = array(\SimpleSAML\Utils\HTTP::getSelfURLHost());
 $attributes['Utilities_selfURLNoQuery()'] = array(\SimpleSAML\Utils\HTTP::getSelfURLNoQuery());
 $attributes['Utilities_getSelfHostWithPath()'] = array(\SimpleSAML\Utils\HTTP::getSelfHostWithPath());