Index: src/wp-includes/ms-load.php
===================================================================
--- src/wp-includes/ms-load.php	(revision 27110)
+++ src/wp-includes/ms-load.php	(working copy)
@@ -134,6 +134,92 @@
 }
 
 /**
+ * Retrieve a network object by its domain and path.
+ *
+ * @since 3.9.0
+ *
+ * @param string $domain Domain to check.
+ * @param string $path   Path to check.
+ * @return object|bool Network object if successful. False when no network is found.
+ */
+function get_network_by_path( $domain, $path ) {
+	global $wpdb;
+
+	$network_id = false;
+
+	$domains = array( $domain );
+	$pieces = explode( '.', $domain );
+	// It's possible one domain to search is 'com', but it might as well
+	// be 'localhost' or some other locally mapped domain.
+	while ( array_shift( $pieces ) ) {
+		if ( $pieces ) {
+			$domains[] = implode( '.', $pieces );
+		}
+	}
+
+	if ( '/' !== $path ) {
+		$paths = array( '/', $path );
+	} else {
+		$paths = array( '/' );
+	}
+
+	$domains = "'" . implode( "', '", esc_sql( $domains ) ) . "'";
+	$paths = "'" . implode( "', '", esc_sql( $paths ) ) . "'";
+
+	$networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
+		WHERE domain IN ($domains) AND path IN ($paths)
+		ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
+
+	/*
+	 * Domains are sorted by length of domain, then by length of path.
+	 * The domain must match for the path to be considered. Otherwise,
+	 * a network with the path of / will suffice.
+	 */
+	$found = false;
+	foreach ( $networks as $network ) {
+		if ( $network->domain === $domain ) {
+			if ( $network->path === $path ) {
+				$found = true;
+				break;
+			}
+			if ( $network->path === '/' ) {
+				$found = true;
+				break;
+			}
+		}
+	}
+
+	if ( $found ) {
+		$network = wp_get_network( $network );
+
+		return $network;
+	}
+
+	return false;
+}
+
+/**
+ * Retrieve an object containing information about the requested network.
+ *
+ * @since 3.9.0
+ *
+ * @param int $network_id The network's DB row or ID.
+ * @return mixed Object containing network information if found, false if not.
+ */
+function wp_get_network( $network ) {
+	global $wpdb;
+
+	if ( ! is_object( $network ) ) {
+		$network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
+		if ( ! $network ) {
+			return false;
+		}
+	}
+
+	return $network;
+}
+
+/**
  * Sets current_site object.
  *
  * @access private
@@ -141,7 +227,7 @@
  * @return object $current_site object
  */
 function wpmu_current_site() {
-	global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
+	global $current_site, $domain, $path;
 
 	if ( empty( $current_site ) )
 		$current_site = new stdClass;
@@ -154,77 +240,50 @@
 			$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.' )
-			$current_site->cookie_domain = substr( $current_site->domain, 4 );
-		else
-			$current_site->cookie_domain = $current_site->domain;
 
 		wp_load_core_site_options( $current_site->id );
-
+		$current_site = get_current_site_name( $current_site );
 		return $current_site;
 	}
 
 	$current_site = wp_cache_get( 'current_site', 'site-options' );
-	if ( $current_site )
+	if ( $current_site ) {
+		wp_load_core_site_options();
+		$current_site = get_current_site_name( $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];
+	// One network is most common.
+	$networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" );
+	if ( count( $networks ) <= 1 ) {
+		$current_site = wp_get_network( $networks[0]->id );
+
 		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;
-	}
-	$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 ) );
+		$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 ) );
 
-	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 ) );
+		wp_cache_set( 'current_site', 'site-options' );
 	}
 
-	if ( $current_site ) {
-		$path = $current_site->path;
-		$current_site->cookie_domain = $cookie_domain;
-		return $current_site;
-	}
+	// Multiple networks in play. Determine which using the requested domain and path.
 
-	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;
-		}
+	// Find the first complete path after the domain.
+	$path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
 
-		$current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
-	}
+	$current_site = get_network_by_path( $domain, $path );
 
-	if ( $current_site || defined( 'WP_INSTALLING' ) ) {
-		$path = '/';
+	if ( $current_site ) {
+		$path = $current_site->path;
+		wp_load_core_site_options( $current_site->id );
+		$current_site = get_current_site_name( $current_site );
 		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.' ) );
 }
 
 /**
Index: src/wp-includes/ms-settings.php
===================================================================
--- src/wp-includes/ms-settings.php	(revision 27110)
+++ src/wp-includes/ms-settings.php	(working copy)
@@ -37,9 +37,6 @@
 	}
 
 	$domain = rtrim( $domain, '.' );
-	$cookie_domain = $domain;
-	if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
-		$cookie_domain = substr( $cookie_domain, 4 );
 
 	$path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
 	$path = str_replace ( '/wp-admin/', '/', $path );
@@ -46,6 +43,11 @@
 	$path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
 
 	$current_site = wpmu_current_site();
+	$current_site->cookie_domain = $current_site->domain;
+	if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
+		$current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
+	}
+
 	if ( ! isset( $current_site->blog_id ) )
 		$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 ) );
 
Index: tests/phpunit/includes/factory.php
===================================================================
--- tests/phpunit/includes/factory.php	(revision 27110)
+++ tests/phpunit/includes/factory.php	(working copy)
@@ -42,6 +42,11 @@
 	 */
 	public $blog;
 
+	/**
+	 * @var WP_UnitTest_Factory_For_Network
+	 */
+	public $network;
+
 	function __construct() {
 		$this->post = new WP_UnitTest_Factory_For_Post( $this );
 		$this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
@@ -50,8 +55,10 @@
 		$this->term = new WP_UnitTest_Factory_For_Term( $this );
 		$this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
 		$this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
-		if ( is_multisite() )
+		if ( is_multisite() ) {
 			$this->blog = new WP_UnitTest_Factory_For_Blog( $this );
+			$this->network = new WP_UnitTest_Factory_For_Network( $this );
+		}
 	}
 }
 
@@ -177,6 +184,39 @@
 }
 
 
+class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
+
+	function __construct( $factory = null ) {
+		parent::__construct( $factory );
+		$this->default_generation_definitions = array(
+			'domain' => WP_TESTS_DOMAIN,
+			'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
+			'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
+			'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
+			'subdomain_install' => false,
+		);
+	}
+
+	function create_object( $args ) {
+		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
+
+		if ( ! isset( $args['user'] ) ) {
+			$email = WP_TESTS_EMAIL;
+		} else {
+			$email = get_userdata( $args['user'] )->user_email;
+		}
+
+		populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
+		return $args['network_id'];
+	}
+
+	function update_object( $network_id, $fields ) {}
+
+	function get_object_by_id( $network_id ) {
+		return wp_get_network( $network_id );
+	}
+}
+
 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
 
 	private $taxonomy;
Index: tests/phpunit/tests/ms.php
===================================================================
--- tests/phpunit/tests/ms.php	(revision 27110)
+++ tests/phpunit/tests/ms.php	(working copy)
@@ -1172,6 +1172,44 @@
 		// Expect 0 sites when using an offset larger than the number of sites
 		$this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) );
 	}
+
+	/**
+	 * @ticket 27003
+	 */
+	function test_get_network_by_path() {
+		global $wpdb;
+
+		$ids = array(
+			'wordpress.org/'           => array( 'domain' => 'wordpress.org', 'path' => '/' ),
+			'wordpress.net/'           => array( 'domain' => 'wordpress.net', 'path' => '/' ),
+			'www.wordpress.net/'       => array( 'domain' => 'www.wordpress.net', 'path' => '/' ),
+			'wordpress.org/one/'       => array( 'domain' => 'wordpress.org', 'path' => '/one/' ),
+			'www.wordpress.org/two/'   => array( 'domain' => 'www.wordpress.org', 'path' => '/two/' ),
+			'www.wordpress.net/three/' => array( 'domain' => 'www.wordpress.net', 'path' => '/three/' ),
+			'wordpress.net/four/'      => array( 'domain' => 'wordpress.net', 'path' => '/four/' ),
+		);
+
+		foreach ( $ids as &$id ) {
+			$id = $this->factory->network->create( $id );
+		}
+		unset( $id );
+
+		$this->assertEquals( $ids['www.wordpress.net/'],
+			get_network_by_path( 'www.wordpress.net', '/notapath/' )->id );
+
+		$this->assertEquals( $ids['www.wordpress.net/three/'],
+			get_network_by_path( 'www.wordpress.net', '/three/' )->id );
+
+		// This won't find /four/ because of the www.
+		$this->assertEquals( $ids['www.wordpress.net/'],
+			get_network_by_path( 'www.wordpress.net', '/four/' )->id );
+
+		$this->assertEquals( $ids['wordpress.net/four/'],
+			get_network_by_path( 'wordpress.net', '/four/' )->id );
+
+		$this->assertEquals( $ids['wordpress.net/'],
+			get_network_by_path( 'wordpress.net', '/notapath/' )->id );
+	}
 }
 
 endif;
