diff --git a/lib/base.php b/lib/base.php
index b526840a97d87b4697af5a4ea7ad49950f69c9bc..840d904471175b0d1f6f22d7b7fd677fd8a2804f 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -117,10 +117,18 @@ class OC {
 			}
 		}
 
-		OC::$WEBROOT = substr($scriptName, 0, strlen($scriptName) - strlen(OC::$SUBURI));
+		if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) {
+			OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI));
 
-		if (OC::$WEBROOT != '' and 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', '');
 		}
 
 		// search the 3rdparty folder