diff --git a/lib/base.php b/lib/base.php
index 6048df33f43b6b29e447fbb71ec04ace4c3830f6..1ffeb7bdec4b616b692f38aa74f0cbe24ce338a7 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -152,18 +152,22 @@ class OC {
 			}
 		}
 
-		if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) {
-			OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI));
+		if (OC::$CLI) {
+			OC::$WEBROOT = OC_Config::getValue('overwritewebroot', '');
+		} else {
+			if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) {
+				OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI));
 
-			if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') {
-				OC::$WEBROOT = '/' . OC::$WEBROOT;
+				if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') {
+					OC::$WEBROOT = '/' . OC::$WEBROOT;
+				}
+			} else {
+				// The scriptName is not ending with OC::$SUBURI
+				// This most likely means that we are calling from CLI.
+				// However some cron jobs still need to generate
+				// a web URL, so we use overwritewebroot as a fallback.
+				OC::$WEBROOT = OC_Config::getValue('overwritewebroot', '');
 			}
-		} else {
-			// The scriptName is not ending with OC::$SUBURI
-			// This most likely means that we are calling from CLI.
-			// However some cron jobs still need to generate
-			// a web URL, so we use overwritewebroot as a fallback.
-			OC::$WEBROOT = OC_Config::getValue('overwritewebroot', '');
 		}
 
 		// search the 3rdparty folder
@@ -516,6 +520,8 @@ class OC {
 		spl_autoload_register(array(self::$loader, 'load'));
 		$loaderEnd = microtime(true);
 
+		self::$CLI = (php_sapi_name() == 'cli');
+
 		self::initPaths();
 
 		// setup 3rdparty autoloader
@@ -541,7 +547,6 @@ class OC {
 		if (defined('DEBUG') && DEBUG) {
 			ini_set('display_errors', 1);
 		}
-		self::$CLI = (php_sapi_name() == 'cli');
 
 		date_default_timezone_set('UTC');
 		ini_set('arg_separator.output', '&');
diff --git a/lib/private/appframework/http/request.php b/lib/private/appframework/http/request.php
index dad5cb050ca8f54b454b41f74fbb1b5fc3958b4e..959ea273280830d183ac428e4a8880c50baf4980 100644
--- a/lib/private/appframework/http/request.php
+++ b/lib/private/appframework/http/request.php
@@ -625,7 +625,7 @@ class Request implements \ArrayAccess, \Countable, IRequest {
 	 * @return string Server host
 	 */
 	public function getInsecureServerHost() {
-		$host = null;
+		$host = 'localhost';
 		if (isset($this->server['HTTP_X_FORWARDED_HOST'])) {
 			if (strpos($this->server['HTTP_X_FORWARDED_HOST'], ',') !== false) {
 				$parts = explode(',', $this->server['HTTP_X_FORWARDED_HOST']);