Index: src/wp-includes/ms-settings.php
===================================================================
--- src/wp-includes/ms-settings.php	(revision 27428)
+++ src/wp-includes/ms-settings.php	(working copy)
@@ -67,7 +67,7 @@
 	} elseif ( ! is_subdomain_install() ) {
 		/*
 		 * A "subdomain" install can be re-interpreted to mean "can support any domain".
-		 * If we're not dealing with one of these installs, then the important part is determing
+		 * If we're not dealing with one of these installs, then the important part is determining
 		 * the network first, because we need the network's path to identify any sites.
 		 */
 		if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
@@ -108,9 +108,44 @@
 		$current_site = wp_get_network( $current_blog->site_id );
 	}
 
-	// If we don't have a network by now, we have a problem.
+	// If a site has not been found and we are installing multisite, use a temporary ID.
+	if ( empty( $current_blog ) && defined( 'WP_INSTALLING' ) ) {
+		$current_blog->blog_id = $blog_id = 1;
+	}
+
 	if ( empty( $current_site ) ) {
+		// No network has been found. Without a network, no site can exist.
 		ms_not_installed();
+	} elseif ( empty( $current_blog ) && ! defined( 'WP_INSTALLING' ) ) {
+		// A network is available, but we have not found the requested site on the network. Redirect if possible.
+		// @todo site_not_found action to provide custom redirect. see #21143
+		if ( is_subdomain_install() ) {
+			// In a subdomain with closed registration, administrators are instructed to set
+			// NOBLOGREDIRECT to redirect visitors who visit an invalid subdomain. If this
+			// is not set, we assume a redirect to wp-signup.php is intentional.
+			if ( defined( 'NOBLOGREDIRECT' ) ) {
+				if ( '%siteurl%' === NOBLOGREDIRECT ) {
+					// network_home_url() is not available to us yet.
+					$destination = "http://" . $current_site->domain . $current_site->path;
+				} else {
+					$destination = NOBLOGREDIRECT;
+				}
+			} else {
+				$destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
+			}
+			header( 'Location: ' . $destination );
+			exit;
+		} elseif ( 0 !== strcasecmp( $current_site->domain, $domain ) ) {
+			// If a subdomain was requested for a subdirectory configuration and could not be found,
+			// or if an unfound domain was requested for a subdirectory configuration and could not
+			// be found on the single network, redirect to the network's primary domain and path.
+			header( 'Location: http://' . $current_site->domain . $current_site->path );
+			exit;
+		}
+
+		// Multiple networks exist in a subdirectory installation and the requested
+		// domain does not match a site on any of them.
+		ms_not_installed();
 	}
 
 	// @todo What if the domain of the network doesn't match the current site?
@@ -130,34 +165,6 @@
 		}
 	}
 
-	// If we haven't figured out our site, give up.
-	if ( empty( $current_blog ) ) {
-		if ( defined( 'WP_INSTALLING' ) ) {
-			$current_blog->blog_id = $blog_id = 1;
-
-		} elseif ( is_subdomain_install() ) {
-			// @todo This is only for an open registration subdomain network.
-			if ( defined( 'NOBLOGREDIRECT' ) ) {
-				if ( '%siteurl%' === NOBLOGREDIRECT ) {
-					$destination = "http://" . $current_site->domain . $current_site->path;
-				} else {
-					$destination = NOBLOGREDIRECT;
-				}
-			} else {
-				$destination = 'http://' . $current_site->domain . $current_site->path . 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
-			}
-			header( 'Location: ' . $destination );
-			exit;
-
-		} else {
-			if ( 0 !== strcasecmp( $current_site->domain, $domain ) ) {
-				header( 'Location: http://' . $current_site->domain . $current_site->path );
-				exit;
-			}
-			ms_not_installed();
-		}
-	}
-
 	$blog_id = $current_blog->blog_id;
 	$public  = $current_blog->public;
 
