diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index cb0170c5cb225ed9c9c3b15beaafecb949fb412d..f2c7144460fa363987982af71d971a53c6ee8e74 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -157,16 +157,18 @@ class SimpleSAML_Utilities {
 
 
 	/**
-	 * Will return sp.example.org/foo
+	 * Will return sp.example.org/ssp/sp1
+	 *
+	 * Please note this function will return the base URL for the current
+	 * SP, as defined in the global configuration.
 	 */
 	public static function getSelfHostWithPath() {
 	
+		$baseurl = explode("/", self::getBaseURL());
+		$elements = array_slice($baseurl, 3 - count($baseurl), count($baseurl) - 4);
+		$path = implode("/", $elements);
 		$selfhostwithpath = self::getSelfHost();
-		if (preg_match('|^(/.*?)/|', $_SERVER['SCRIPT_NAME'], $matches)) {
-			$selfhostwithpath .= $matches[1];
-		}
-		return $selfhostwithpath;
-	
+		return $selfhostwithpath . "/" . $path;
 	}
 	
 	/**