Index: src/wp-includes/ms-load.php
===================================================================
--- src/wp-includes/ms-load.php	(revision 27114)
+++ src/wp-includes/ms-load.php	(working copy)
@@ -134,6 +134,89 @@
 }
 
 /**
+ * Retrieve a network object by its domain and path.
+ *
+ * @since 3.9.0
+ *
+ * @param string $domain
+ * @param string $path
+ *
+ * @return mixed
+ */
+function get_network_by_path( $domain, $path ) {
+	global $wpdb;
+
+	$network_id = false;
+
+	while ( 1 <= substr_count( $domain, '.' ) ) {
+		// Check for a match on the full domain and first path
+		$network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
+
+		if ( $network_id ) {
+			break;
+		}
+
+		// Check for a match on the full domain and / path
+		if ( $path !== '/' ) {
+			$network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = '/'", $domain ) );
+		}
+
+		if ( $network_id ) {
+			break;
+		}
+
+		$domain = substr( $domain, 1 + strpos( $domain, '.' ) );
+
+		// Check for a match on the root domain and first path
+		$network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
+
+		if ( $network_id ) {
+			break;
+		}
+
+		if ( $path !== '/' ) {
+			$network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = '/'", $domain ) );
+		}
+
+		if ( $network_id ) {
+			break;
+		}
+
+	}
+
+	if ( $network_id ) {
+		$network = wp_get_network( $network_id );
+
+		return $network;
+	}
+
+	return false;
+}
+
+/**
+ * Retrieve an object containing information about the requested network.
+ *
+ * @since 3.9.0
+ *
+ * @param int $network_id        The network's ID
+ *
+ * @return mixed Object containing network information if found, false if not.
+ */
+function wp_get_network( $network_id ) {
+	global $wpdb;
+
+	$network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network_id ) );
+
+	if ( $network ) {
+		wp_load_core_site_options( $network->id );
+		$network = get_current_site_name( $network );
+		return $network;
+	}
+
+	return false;
+}
+
+/**
  * Sets current_site object.
  *
  * @access private
@@ -141,7 +224,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;
@@ -170,61 +253,19 @@
 	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 ( ! $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 ) );
-	}
-
+	$current_site = get_network_by_path( $domain, $path );
 	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.' ) );
 }
 
 /**
