diff --git a/core/includes/install.inc b/core/includes/install.inc
index 514d89c..991bbfd 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -216,7 +216,7 @@ function drupal_detect_baseurl($file = 'core/install.php') {
   $proto = $_SERVER['HTTPS'] ? 'https://' : 'http://';
   $host = $_SERVER['SERVER_NAME'];
   $port = ($_SERVER['SERVER_PORT'] == 80 ? '' : ':' . $_SERVER['SERVER_PORT']);
-  $uri = preg_replace("/\?.*/", '', $_SERVER['REQUEST_URI']);
+  $uri = preg_replace("/\?.*/", '', request_uri());
   $dir = str_replace("/$file", '', $uri);
 
   return "$proto$host$port$dir";
diff --git a/core/modules/simpletest/tests/common.test b/core/modules/simpletest/tests/common.test
index 78da5e4..77b7e6c 100644
--- a/core/modules/simpletest/tests/common.test
+++ b/core/modules/simpletest/tests/common.test
@@ -1827,6 +1827,57 @@ class DrupalRenderTestCase extends DrupalWebTestCase {
 }
 
 /**
+ * Test for drupal_detect_baseurl().
+ *
+ * @see http://drupal.org/node/226873
+ * "Since $_SERVER['REQUEST_URI'] is only available on Apache, we generate an
+ * equivalent using other environment variables."
+ */
+class ValidDetectBaseUrlTestCase extends DrupalUnitTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Valid Base Url',
+      'description' => "Performs tests on Drupal's valid base url function.",
+      'group' => 'System'
+    );
+  }
+
+  function setUp() {
+    // Save $_SERVER variable.
+    $this->oldserver = $_SERVER;
+
+    $_SERVER['SCRIPT_NAME'] = 'index.php';
+    $_SERVER['QUERY_STRING'] = 'foo=bar';
+
+    if (isset($_SERVER['REQUEST_URI'])) {
+      unset($_SERVER['REQUEST_URI']);
+    }
+
+    parent::setUp();
+  }
+
+  function tearDown() {
+    // Restore $_SERVER variable.
+    $_SERVER = $this->oldserver;
+    parent::tearDown();
+  }
+  
+  /**
+   * Test valid base urls.
+   */
+  function testValidBaseUrl() {
+    include 'core/includes/install.inc';
+
+    $url = drupal_detect_baseurl();
+    $request_uri = $GLOBALS['base_url'] . request_uri();
+
+    $parts = explode('?', $request_uri);
+    $request_uri = $parts[0];
+    $this->assertEqual($url, $request_uri, t('The base URL detecting is valid.'));
+  }
+}
+
+/**
  * Test for valid_url().
  */
 class ValidUrlTestCase extends DrupalUnitTestCase {
