Index: src/wp-includes/ms-load.php
===================================================================
--- src/wp-includes/ms-load.php	(revision 27087)
+++ src/wp-includes/ms-load.php	(working copy)
@@ -134,6 +134,44 @@
 }
 
 /**
+ * Retrieve an object containing information about the requested network.
+ *
+ * @since 3.9.0
+ *
+ * @param string $domain        The network's domain.
+ * @param string $path          The primary path of the network.
+ * @param string $cookie_domain The domain to be used for cookies on the network.
+ *
+ * @return mixed Object containing network information if found, false if not.
+ */
+function wp_get_network( $domain, $path, $cookie_domain = '' ) {
+	global $wpdb;
+
+	if ( empty( $cookie_domain ) ) {
+		if ( 'www.' === substr( $domain, 0, 4 ) ) {
+			$cookie_domain = substr( $domain, 4 );
+		} else {
+			$cookie_domain = $domain;
+		}
+	}
+
+	if ( $domain === $cookie_domain ) {
+		$network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
+	} else {
+		$network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) );
+	}
+
+	if ( $network ) {
+		wp_load_core_site_options( $network->id );
+		$network = get_current_site_name( $network );
+		$network->cookie_domain = $cookie_domain;
+		return $network;
+	}
+
+	return false;
+}
+
+/**
  * Sets current_site object.
  *
  * @access private
@@ -141,7 +179,7 @@
  * @return object $current_site object
  */
 function wpmu_current_site() {
-	global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
+	global $current_site, $domain, $path, $cookie_domain;
 
 	if ( empty( $current_site ) )
 		$current_site = new stdClass;
@@ -150,10 +188,12 @@
 		$current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
 		$current_site->domain = DOMAIN_CURRENT_SITE;
 		$current_site->path   = $path = PATH_CURRENT_SITE;
+
 		if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
 			$current_site->blog_id = BLOG_ID_CURRENT_SITE;
 		elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
 			$current_site->blog_id = BLOGID_CURRENT_SITE;
+
 		if ( DOMAIN_CURRENT_SITE == $domain )
 			$current_site->cookie_domain = $cookie_domain;
 		elseif ( substr( $current_site->domain, 0, 4 ) == 'www.' )
@@ -170,61 +210,31 @@
 	if ( $current_site )
 		return $current_site;
 
-	$sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
-	if ( 1 == count( $sites ) ) {
-		$current_site = $sites[0];
-		wp_load_core_site_options( $current_site->id );
-		$path = $current_site->path;
-		$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
-		$current_site = get_current_site_name( $current_site );
-		if ( substr( $current_site->domain, 0, 4 ) == 'www.' )
-			$current_site->cookie_domain = substr( $current_site->domain, 4 );
-		wp_cache_set( 'current_site', $current_site, 'site-options' );
-		return $current_site;
-	}
+	// Find the first complete path after the domain.
 	$path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
 
-	if ( $domain == $cookie_domain )
-		$current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
-	else
-		$current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) );
+	// If we expect this to be a subdomain install, strip the first part of the domain for the network lookup.
+	if ( is_subdomain_install() ) {
+		$domain = substr( $domain, 1 + strpos( $domain, '.' ) );
+	}
 
+	$current_site = wp_get_network( $domain, $path, $cookie_domain );
+
+	// Repeat the network search with an empty path.
 	if ( ! $current_site ) {
-		if ( $domain == $cookie_domain )
-			$current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain ) );
-		else
-			$current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = '/' ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain ) );
+		$path = '/';
+		$current_site = wp_get_network( $domain, $path, $cookie_domain );
 	}
 
 	if ( $current_site ) {
 		$path = $current_site->path;
-		$current_site->cookie_domain = $cookie_domain;
-		return $current_site;
-	}
+		wp_cache_set( 'current_site', $current_site, 'site-options' );
 
-	if ( is_subdomain_install() ) {
-		$sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) );
-		$current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
-		if ( $current_site ) {
-			$current_site->cookie_domain = $current_site->domain;
-			return $current_site;
-		}
-
-		$current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
-	}
-
-	if ( $current_site || defined( 'WP_INSTALLING' ) ) {
-		$path = '/';
 		return $current_site;
 	}
 
-	// Still no dice.
 	wp_load_translations_early();
-
-	if ( 1 == count( $sites ) )
-		wp_die( sprintf( __( 'That site does not exist. Please try <a href="%s">%s</a>.' ), 'http://' . $sites[0]->domain . $sites[0]->path ) );
-	else
-		wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
+	wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
 }
 
 /**
