diff --git a/include/class.misc.php b/include/class.misc.php
index d49970e9f9ca8a92d5e16e29c146f329ad7bda60..27e259e330da365c96979017677c4cce695e4d61 100644
--- a/include/class.misc.php
+++ b/include/class.misc.php
@@ -139,25 +139,5 @@ class Misc {
         return $output;
     }
 
-    /* static */
-    function siteRootPath($main_inc_path) {
-        if (!$_SERVER['DOCUMENT_ROOT'])
-            // Probably run from the command-line
-            return './';
-        $root = str_replace('\\', '/', $main_inc_path);
-        $root2 = str_replace('\\','/', $_SERVER['DOCUMENT_ROOT']);
-        $path = '';
-        while (strpos($_SERVER['DOCUMENT_ROOT'], $root) === false) {
-            $lastslash = strrpos($root, '/');
-            if ($lastslash === false)
-                // Unable to find any commonality between $root and
-                // DOCUMENT_ROOT
-                return './';
-            $path = substr($root, $lastslash) . $path;
-            $root = substr($root, 0, $lastslash);
-        }
-        return $path;
-    }
-
 }
 ?>
diff --git a/include/class.osticket.php b/include/class.osticket.php
index 8755d3278fda537bd02e4454251b35a1dcbc1e58..cca754b4ef160f2d33370118ec55a8e7165fd768 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -352,6 +352,82 @@ class osTicket {
         return null;
     }
 
+    /* static */
+    function get_root_path($dir) {
+
+        /* If run from the commandline, DOCUMENT_ROOT will not be set. It is
+         * also likely that the ROOT_PATH will not be necessary, so don't
+         * bother attempting to figure it out.
+         *
+         * Secondly, if the directory of main.inc.php is the same as the
+         * document root, the the ROOT path truly is '/'
+         */
+        if(!$_SERVER['DOCUMENT_ROOT']
+                || !strcasecmp($_SERVER['DOCUMENT_ROOT'], $dir))
+            return '/';
+
+        /* If DOCUMENT_ROOT is set and isn't the same as the directory for
+         * main.inc.php, then assume that the two have something in common.
+         * For instance, you might have the following configurations
+         *
+         * +-----------------+-----------------------+------------+----------+
+         * | DOCUMENT_ROOT   | dirname(main.inc.php) | ROOT_PATH  | Comments |
+         * +-----------------+-----------------------+------------+----------+
+         * | /var/www        | /var/www/osticket     | /osticket/ | vanilla  |
+         * | /srv/httpd/www  | /httpd/www            | /          | chrooted |
+         * | /srv/httpd/www  | /httpd/www/osticket   | /osticket/ | chrooted |
+         * +-----------------+-----------------------+------------+----------+
+         *
+         * This algorithm will walk the two paths right to left, chipping
+         * away at the path of main.inc.php. When the two paths are equal,
+         * the part removed from the main.inc.php path is the ROOT_PATH
+         */
+        $dir = str_replace('\\', '/', $dir);
+        $root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
+
+        // Not chrooted
+        if(strpos($dir, $root)!==false)
+            return substr($dir, strlen($dir));
+
+        // Chrooted ?
+        $path = '';
+        while (strpos($root, $dir) === false) {
+            $lastslash = strrpos($dir, '/');
+            $path = substr($dir, $lastslash) . $path;
+            $dir = substr($dir, 0, $lastslash);
+            if (!$dir)
+                break;
+        }
+
+        if($dir && $path)
+            return $path;
+
+        /* The last resort is to try and use SCRIPT_FILENAME and
+         * SCRIPT_NAME. The SCRIPT_FILENAME server variable should be the
+         * full path of the originally-executed-script. The SCRIPT_NAME
+         * should be the path of that script inside the DOCUMENT_ROOT. This
+         * is most likely useful if osTicket is run using something like
+         * Apache UserDir setting where the DOCUMENT_ROOT of Apache and the
+         * installation path of osTicket have nothing in comon.
+         *
+         * +---------------------------+-------------------+----------------+
+         * | SCRIPT_FILENAME           | SCRIPT_NAME       | ROOT_PATH      |
+         * +---------------------------+-------------------+----------------+
+         * | /home/u1/www/osticket/... | /~u1/osticket/... | /~u1/osticket/ |
+         * +---------------------------+-------------------+----------------+
+         *
+         * The algorithm will remove the directory of main.inc.php from
+         * SCRIPT_FILENAME. What's left should be the script executed inside
+         * the osTicket installation. That is removed from SCRIPT_NAME.
+         * What's left is the ROOT_PATH.
+         */
+        $path = substr($_SERVER['SCRIPT_FILENAME'], strlen(ROOT_DIR));
+        if($path  && ($pos=strpos($_SERVER['SCRIPT_NAME'], $path))!==false)
+            return substr($_SERVER['SCRIPT_NAME'], 0, $pos);
+
+        return null;
+    }
+
     /**
      * Returns TRUE if the request was made via HTTPS and false otherwise
      */
diff --git a/include/ost-sampleconfig.php b/include/ost-sampleconfig.php
index 65ff21c0d053e78c6b49a1e0647b881a1bff57f3..a5f896769fb81a931571e9afa5f98c0a8ef81967 100644
--- a/include/ost-sampleconfig.php
+++ b/include/ost-sampleconfig.php
@@ -16,8 +16,22 @@
     $Id: $
 **********************************************************************/
 
+/**
+ * If you have a strange HTTP server configuration and osTicket cannot
+ * discover the URL path of where your osTicket is installed, define
+ * ROOT_PATH here.
+ *
+ * The ROOT_PATH is the part of the URL used to access your osTicket
+ * helpdesk before the '/scp' part and after the hostname. For instance, for
+ * http://mycompany.com/support', the ROOT_PATH should be '/support/'
+ *
+ * ROOT_PATH *must* end with a forward-slash!
+ */
+# define('ROOT_PATH', '/support/');
+
 #Disable direct access.
-if(!strcasecmp(basename($_SERVER['SCRIPT_NAME']),basename(__FILE__)) || !defined('ROOT_PATH')) die('kwaheri rafiki!');
+if(!strcasecmp(basename($_SERVER['SCRIPT_NAME']),basename(__FILE__)) || !defined('INCLUDE_DIR'))
+    die('kwaheri rafiki!');
 
 #Install flag
 define('OSTINSTALLED',FALSE);
diff --git a/main.inc.php b/main.inc.php
index 323a0a4550cbf2fed30e9e8843c37bc7b5f3451b..ce874b26cb6920ff9e08fdcf9e1ce22e143cbd96 100644
--- a/main.inc.php
+++ b/main.inc.php
@@ -68,17 +68,19 @@
     define('UPGRADE_DIR', INCLUDE_DIR.'upgrader/');
     define('I18N_DIR', INCLUDE_DIR.'i18n/');
 
-    require(INCLUDE_DIR.'class.misc.php');
-
-    // Determine the path in the URI used as the base of the osTicket
-    // installation
-    if (!defined('ROOT_PATH'))
-        define('ROOT_PATH', Misc::siteRootPath(realpath(dirname(__file__))).'/'); //root path. Damn directories
-
     /*############## Do NOT monkey with anything else beyond this point UNLESS you really know what you are doing ##############*/
 
     #Current version && schema signature (Changes from version to version)
     define('THIS_VERSION','1.7.0+'); //Shown on admin panel
+
+
+    require(INCLUDE_DIR.'class.osticket.php');
+
+    // Determine the path in the URI used as the base of the osTicket
+    // installation
+    if (!defined('ROOT_PATH') && ($rp = osTicket::get_root_path(dirname(__file__))))
+        define('ROOT_PATH', rtrim($rp, '/').'/');
+
     #load config info
     $configfile='';
     if(file_exists(ROOT_DIR.'ostconfig.php')) //Old installs prior to v 1.6 RC5
@@ -98,6 +100,10 @@
     require($configfile);
     define('CONFIG_FILE',$configfile); //used in admin.php to check perm.
 
+    //Die if root path is not defined
+    if(!defined('ROOT_PATH') || !ROOT_PATH)
+        die("<b>Fatal Error:</b> unknown root path. Define it in your 'ost-config.php'");
+
    //Path separator
     if(!defined('PATH_SEPARATOR')){
         if(strpos($_ENV['OS'],'Win')!==false || !strcasecmp(substr(PHP_OS, 0, 3),'WIN'))
@@ -111,7 +117,7 @@
 
 
     #include required files
-    require(INCLUDE_DIR.'class.osticket.php');
+    require(INCLUDE_DIR.'class.misc.php');
     require(INCLUDE_DIR.'class.ostsession.php');
     require(INCLUDE_DIR.'class.usersession.php');
     require(INCLUDE_DIR.'class.pagenate.php'); //Pagenate helper!