diff --git a/includes/install.inc b/includes/install.inc
index 71de3e6..e232870 100644
--- a/includes/install.inc
+++ b/includes/install.inc
@@ -222,7 +222,7 @@ function drupal_detect_baseurl($file = '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/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test
index 44eecdc..52112da 100644
--- a/modules/simpletest/tests/common.test
+++ b/modules/simpletest/tests/common.test
@@ -1925,6 +1925,57 @@ class DrupalRenderTestCase extends DrupalWebTestCase {
 }
 
 /**
+ * Tests for drupal_detect_baseurl().
+ *
+ * Since $_SERVER['REQUEST_URI'] is only available on Apache,
+ * rupal_detect_baseurl() uses request_uri() to 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 '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 detected base URL is valid.'));
+  }
+}
+
+/**
  * Test for valid_url().
  */
 class ValidUrlTestCase extends DrupalUnitTestCase {
