Index: src/wp-includes/class-wp-network.php
===================================================================
--- src/wp-includes/class-wp-network.php	(revision 39646)
+++ src/wp-includes/class-wp-network.php	(working copy)
@@ -267,8 +267,6 @@
 	 * @return WP_Network|bool Network object if successful. False when no network is found.
 	 */
 	public static function get_by_path( $domain = '', $path = '', $segments = null ) {
-		global $wpdb;
-
 		$domains = array( $domain );
 		$pieces  = explode( '.', $domain );
 
@@ -295,7 +293,11 @@
 		if ( wp_using_ext_object_cache() ) {
 			$using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
 			if ( false === $using_paths ) {
-				$using_paths = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} WHERE path <> '/' LIMIT 1" );
+				$using_paths = get_networks( array(
+					'number'       => 1,
+					'count'        => true,
+					'path__not_in' => '/',
+				) );
 				wp_cache_add( 'networks_have_paths', $using_paths, 'site-options'  );
 			}
 		}
@@ -356,32 +358,31 @@
 		// @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
 		// We already have paths covered. What about how far domains should be drilled down (including www)?
 
-		$search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
-
 		if ( ! $using_paths ) {
-			$network = $wpdb->get_row( "
-				SELECT * FROM {$wpdb->site}
-				WHERE domain IN ({$search_domains})
-				ORDER BY CHAR_LENGTH(domain)
-				DESC LIMIT 1
-			" );
+			$networks = get_networks( array(
+				'number'     => 1,
+				'orderby'    => array(
+					'domain_length' => 'DESC',
+				),
+				'domain__in' => $domains,
+			) );
 
-			if ( ! empty( $network ) && ! is_wp_error( $network ) ) {
-				return new WP_Network( $network );
+			if ( ! empty( $networks ) ) {
+				return array_shift( $networks );
 			}
 
 			return false;
-
-		} else {
-			$search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
-			$networks = $wpdb->get_results( "
-				SELECT * FROM {$wpdb->site}
-				WHERE domain IN ({$search_domains})
-				AND path IN ({$search_paths})
-				ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC
-			" );
 		}
 
+		$networks = get_networks( array(
+			'orderby'    => array(
+				'domain_length' => 'DESC',
+				'path_length'   => 'DESC',
+			),
+			'domain__in' => $domains,
+			'path__in'   => $paths,
+		) );
+
 		/*
 		 * Domains are sorted by length of domain, then by length of path.
 		 * The domain must match for the path to be considered. Otherwise,
@@ -402,7 +403,7 @@
 		}
 
 		if ( true === $found ) {
-			return new WP_Network( $network );
+			return $network;
 		}
 
 		return false;
Index: tests/phpunit/tests/multisite/bootstrap.php
===================================================================
--- tests/phpunit/tests/multisite/bootstrap.php	(revision 39646)
+++ tests/phpunit/tests/multisite/bootstrap.php	(working copy)
@@ -89,6 +89,48 @@
 	}
 
 	/**
+	 * @ticket 37217
+	 * @dataProvider data_get_network_by_path_not_using_paths
+	 *
+	 * @param string $expected_key The array key associated with expected data for the test.
+	 * @param string $domain       The requested domain.
+	 * @param string $path         The requested path.
+	 * @param string $message      The message to pass for failed tests.
+	 */
+	public function test_get_network_by_path_not_using_paths( $expected_key, $domain, $path, $message ) {
+		// Temporarily store original object cache and using paths values.
+		$using_ext_object_cache_orig = wp_using_ext_object_cache( true );
+		if ( $using_ext_object_cache_orig ) {
+			$using_paths_orig = wp_cache_get( 'networks_have_paths', 'site-options' );
+		}
+
+		wp_cache_set( 'networks_have_paths', 0, 'site-options'  );
+
+		$network = get_network_by_path( $domain, $path );
+
+		// Restore original object cache and using paths values.
+		if ( ! $using_ext_object_cache_orig ) {
+			wp_using_ext_object_cache( $using_ext_object_cache_orig );
+		} else {
+			wp_cache_set( 'networks_have_paths', $using_paths_orig, 'site-options'  );
+		}
+
+		$this->assertEquals( self::$network_ids[ $expected_key ], $network->id, $message );
+	}
+
+	public function data_get_network_by_path_not_using_paths() {
+		return array(
+			array( 'wordpress.org/',         'wordpress.org',       '/',          'A standard domain and path request should work.' ),
+			array( 'wordpress.net/',         'wordpress.net',       '/notapath/', 'A network matching a top level domain should be found regardless of path.' ),
+			array( 'www.wordpress.net/',     'www.wordpress.net',   '/notapath/', 'A network matching a domain should be found regardless of path.' ),
+			array( 'wordpress.org/',         'www.wordpress.org',   '/one/',      'Should find the network despite the www and regardless of path.' ),
+			array( 'wordpress.org/',         'site1.wordpress.org', '/one/',      'Should find the network with the corresponding top level domain regardless of path.' ),
+			array( 'www.wordpress.net/',     'www.wordpress.net',   '/two/',      'A www network can coexist with a non-www network.' ),
+			array( 'make.wordpress.org/',    'make.wordpress.org',  '/notapath/', 'A subdomain network should be found regardless of path.' ),
+		);
+	}
+
+	/**
 	 * @ticket 27003
 	 * @ticket 27927
 	 * @dataProvider data_get_site_by_path
