diff --git a/apps/files_sharing/ajax/email.php b/apps/files_sharing/ajax/email.php
index 163683cd0e3c3bc3624df039983d30dbaffa6659..bdfa369418d0037c5f1b44187d901b387f2418a4 100644
--- a/apps/files_sharing/ajax/email.php
+++ b/apps/files_sharing/ajax/email.php
@@ -7,7 +7,7 @@ $type = (strpos($_POST['file'], '.') === false) ? 'folder' : 'file';
 $subject = $user.' shared a '.$type.' with you';
 $link = $_POST['link'];
 $text = $user.' shared the '.$type.' '.$_POST['file'].' with you. It is available for download here: '.$link;
-$fromaddress = OCP\Config::getUserValue($user, 'settings', 'email', 'sharing-noreply@'.$_SERVER['HTTP_HOST']);
-OC_Mail::send($_POST['toaddress'], $_POST['toaddress'], $subject, $text, $fromaddress, $user);
+$fromaddress = OCP\Config::getUserValue($user, 'settings', 'email', 'sharing-noreply@'.OCP\Util::getServerHost());
+OCP\Util::sendMail($_POST['toaddress'], $_POST['toaddress'], $subject, $text, $fromaddress, $user);
 
-?>
\ No newline at end of file
+?>
diff --git a/apps/user_openid/phpmyid.php b/apps/user_openid/phpmyid.php
index 137daa485a90378f2974f3198604458f66766df3..5aaab64285649274fb23fbe94858fbf8678b9264 100644
--- a/apps/user_openid/phpmyid.php
+++ b/apps/user_openid/phpmyid.php
@@ -1624,7 +1624,7 @@ if (! array_key_exists('idp_url', $profile))
 //Determine the requested URL - DO NOT OVERRIDE
 $profile['req_url'] = sprintf("%s://%s%s",
 		      $proto,
-		      $_SERVER['HTTP_HOST'],
+		      OCP\Util::getServerHost(),
 // 		      $port,//host  already includes the path
 		      $_SERVER["REQUEST_URI"]);
 
diff --git a/apps/user_openid/user_openid.php b/apps/user_openid/user_openid.php
index 8deb42f68c8637acc23bc5612c94a243bb708ffb..3267db3fa0f407f8832dd843afc76ac40f1e3eb0 100644
--- a/apps/user_openid/user_openid.php
+++ b/apps/user_openid/user_openid.php
@@ -39,9 +39,9 @@ class OC_USER_OPENID extends OC_User_Backend {
 		// Get identity from user and redirect browser to OpenID Server
 		$openid = new SimpleOpenID;
 		$openid->SetIdentity($uid);
-		$openid->SetTrustRoot('http://' . $_SERVER["HTTP_HOST"]);
+		$openid->SetTrustRoot('http://' . OCP\Util::getServerHost());
 		if ($openid->GetOpenIDServer()){
-			$openid->SetApprovedURL('http://' . $_SERVER["HTTP_HOST"] . OC::$WEBROOT);      // Send Response from OpenID server to this script
+			$openid->SetApprovedURL('http://' . OCP\Util::getServerHost() . OC::$WEBROOT);      // Send Response from OpenID server to this script
 			$openid->Redirect();     // This will redirect user to OpenID Server
 			exit;
 		}else{
diff --git a/core/lostpassword/index.php b/core/lostpassword/index.php
index 2b87a1eb1115fb9654b7999ddb8bb8ee95729467..db9f842380ea29f1ff534b44492715b3b1c68dcc 100644
--- a/core/lostpassword/index.php
+++ b/core/lostpassword/index.php
@@ -22,7 +22,7 @@ if (isset($_POST['user'])) {
 			$tmpl->assign('link', $link);
 			$msg = $tmpl->fetchPage();
 			$l = OC_L10N::get('core');
-			$from = 'lostpassword-noreply@' . $_SERVER['HTTP_HOST'];
+			$from = 'lostpassword-noreply@' . OC_Helper::serverHost();
 			$r=mail($email, $l->t('Owncloud password reset'), $msg, 'From:' . $from);
 			OC_MAIL::send($email,$_POST['user'],$l->t('ownCloud password reset'),$msg,$from,'ownCloud');
 			echo('sent');
diff --git a/lib/base.php b/lib/base.php
index 30d302b39121e4f219cfd0fbfe9fe59d053aebcd..b2ec38ffdb86bc152bca47d3bc539d2d15223e8c 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -211,9 +211,11 @@ class OC{
 		if( OC_Config::getValue( "forcessl", false )){
 			ini_set("session.cookie_secure", "on");
 			if(!isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] != 'on') {
-				$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
-				header("Location: $url");
-				exit();
+				if(!isset($_SERVER['HTTP_X_FORWARDED_PROTO']) or $_SERVER['HTTP_X_FORWARDED_PROTO']=='http') {
+					$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
+					header("Location: $url");
+					exit();
+				}
 			}
 		}
 	}
diff --git a/lib/ocs.php b/lib/ocs.php
index 536ee754e8438b6d268881c2275ebbc0b76644eb..aba29275a92b14561da7dde3b6a23320ebb0ca8b 100644
--- a/lib/ocs.php
+++ b/lib/ocs.php
@@ -357,11 +357,11 @@ class OC_OCS {
    */
   private static function apiConfig($format) {
     $user=OC_OCS::checkpassword(false);
-    $url=substr($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'],0,-11).'';
+    $url=substr(OC_Helper::serverHost().$_SERVER['SCRIPT_NAME'],0,-11).'';
 
     $xml['version']='1.5';
     $xml['website']='ownCloud';
-    $xml['host']=$_SERVER['HTTP_HOST'];
+    $xml['host']=OC_Helper::serverHost();
     $xml['contact']='';
     $xml['ssl']='false';
     echo(OC_OCS::generatexml($format,'ok',100,'',$xml,'config','',1));
diff --git a/ocs/providers.php b/ocs/providers.php
index e1d6ef7ee1e0304f8f0b0774cb4a4585f67c1cbf..0dad62405888160e8994028a16a63b0cca933fa2 100644
--- a/ocs/providers.php
+++ b/ocs/providers.php
@@ -23,7 +23,7 @@
 
 require_once('../lib/base.php');
 
-$url='http://'.substr($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],0,-17).'ocs/v1.php/';
+$url='http://'.substr(OC_Helper::serverHost().$_SERVER['REQUEST_URI'],0,-17).'ocs/v1.php/';
 
 echo('
 <providers>