diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php
index 50c3209af3a8055878530ac0237552f2337cbbe7..5c1c6e89d9255e540e67275ad4d631b707f22757 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php
@@ -207,7 +207,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandler
         }
 
         // then we look for the hostname
-        $currenthost = \SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); // sp.example.org
+        $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // 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 80bade1c933eebfa647eea52986c8030c4327089..3f863222754b0545ad5f973cc86acd9490eff0dc 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::getSelfHostWithoutPort();
+            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
         } elseif ($set === 'adfs-idp-hosted') {
-            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort().':idp';
+            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':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 7202a73f930fc90763db395d4c6e24ebaf156d3b..e8981169d4984b18b9fb0d0ebd8e8b766b1bd397 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::getSelfHostWithoutPort();
+            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
         } elseif ($set === 'adfs-idp-hosted') {
-            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort().':idp';
+            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':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 d442bf7829a5471ab289cfc96841bd3c00e56eb2..9d677cda5ee9ace272e1df5324b012640f9b2354 100644
--- a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php
+++ b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php
@@ -198,7 +198,7 @@ abstract class SimpleSAML_Metadata_MetaDataStorageSource
         $metadataSet = $this->getMetadataSet($set);
 
         // check for hostname
-        $currenthost = \SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); // sp.example.org
+        $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // 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 c25bc6d57b8d03a91678403ce4c535e7079ab3b1..00946fc3c83d2ab1d0072bb54d343eb2762453b8 100644
--- a/lib/SimpleSAML/Utils/HTTP.php
+++ b/lib/SimpleSAML/Utils/HTTP.php
@@ -595,32 +595,35 @@ class HTTP
     /**
      * Retrieve our own host.
      *
-     * @return string The current host (with non-default ports included).
+     * E.g. www.example.com
      *
-     * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no>
-     * @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
+     * @return string The current host.
+     *
+     * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no>
      */
     public static function getSelfHost()
     {
-        $url = self::getBaseURL();
-
-        $start = strpos($url, '://') + 3;
-        $length = strcspn($url, '/', $start);
-
-        return substr($url, $start, $length);
+        return array_shift(explode(':', self::getSelfHostWithNonStandardPort()));
     }
 
     /**
-     * Retrieve our own host.
+     * Retrieve our own host, including the port in case the it is not standard for the protocol in use. That is port
+     * 80 for HTTP and port 443 for HTTPS.
+     *
+     * E.g. www.example.com:8080
      *
-     * @return string The current host without port specification.
+     * @return string The current host, followed by a colon and the port number, in case the port is not standard for
+     * the protocol.
+     *
+     * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no>
+     * @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
      */
-    public static function getSelfHostWithoutPort()
+    public static function getSelfHostWithNonStandardPort()
     {
         $url = self::getBaseURL();
 
         $start = strpos($url, '://') + 3;
-        $length = strcspn($url, '/:', $start);
+        $length = strcspn($url, '/', $start);
 
         return substr($url, $start, $length);
     }
diff --git a/tests/lib/SimpleSAML/Utils/HTTPTest.php b/tests/lib/SimpleSAML/Utils/HTTPTest.php
index b4e9845a72cfdd0133e655fa448ef431ebd2756f..f67b8832e2925c5cfee07ea4c9c0046c489af1c2 100644
--- a/tests/lib/SimpleSAML/Utils/HTTPTest.php
+++ b/tests/lib/SimpleSAML/Utils/HTTPTest.php
@@ -6,7 +6,7 @@ use SimpleSAML\Utils\HTTP;
 class HTTPTest extends \PHPUnit_Framework_TestCase
 {
     /**
-     * Test HTTP::getSelfHost with and without custom port
+     * Test SimpleSAML\Utils\HTTP::getSelfHost() with and without custom port.
      */
     public function testGetSelfHost()
     {
@@ -16,20 +16,29 @@ class HTTPTest extends \PHPUnit_Framework_TestCase
         $_SERVER['SERVER_PORT'] = '80';
         $this->assertEquals('localhost', HTTP::getSelfHost());
         $_SERVER['SERVER_PORT'] = '3030';
-        $this->assertEquals('localhost:3030', HTTP::getSelfHost());
+        $this->assertEquals('localhost', HTTP::getSelfHost());
     }
 
     /**
-     * Test HTTP::getSelfHostWithoutPort
+     * Test SimpleSAML\Utils\HTTP::getSelfHostWithPort(), with and without custom port.
      */
-    public function testGetSelfHostWithoutPort()
+    public function testGetSelfHostWithPort()
     {
         \SimpleSAML_Configuration::loadFromArray(array(
             'baseurlpath' => '',
         ), '[ARRAY]', 'simplesaml');
+
+        // standard port for HTTP
         $_SERVER['SERVER_PORT'] = '80';
-        $this->assertEquals('localhost', HTTP::getSelfHostWithoutPort());
+        $this->assertEquals('localhost', HTTP::getSelfHostWithNonStandardPort());
+
+        // non-standard port
         $_SERVER['SERVER_PORT'] = '3030';
-        $this->assertEquals('localhost', HTTP::getSelfHostWithoutPort());
+        $this->assertEquals('localhost:3030', HTTP::getSelfHostWithNonStandardPort());
+
+        // standard port for HTTPS
+        $_SERVER['HTTPS'] = 'on';
+        $_SERVER['SERVER_PORT'] = '443';
+        $this->assertEquals('localhost', HTTP::getSelfHostWithNonStandardPort());
     }
 }
diff --git a/www/admin/hostnames.php b/www/admin/hostnames.php
index e77d99edcc751cfc58966ff637ff577cfb01de11..4baec753b180280140b7c6d7e386bc68bb6587fd 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::getSelfHostWithoutPort());
+$attributes['Utilities_getSelfHost()'] = array(\SimpleSAML\Utils\HTTP::getSelfHost());
 $attributes['Utilities_selfURLhost()'] = array(\SimpleSAML\Utils\HTTP::getSelfURLHost());
 $attributes['Utilities_selfURLNoQuery()'] = array(\SimpleSAML\Utils\HTTP::getSelfURLNoQuery());
 $attributes['Utilities_getSelfHostWithPath()'] = array(\SimpleSAML\Utils\HTTP::getSelfHostWithPath());