Index: wp-includes/link-template.php
===================================================================
--- wp-includes/link-template.php	(revision 13890)
+++ wp-includes/link-template.php	(working copy)
@@ -1900,7 +1900,7 @@
 	if ( empty($blog_id) || !is_multisite() )
 		$url = get_option('siteurl');
 	else
-		$url = untrailingslashit(get_blogaddress_by_id($blog_id));
+		$url = untrailingslashit(get_blog_option($blog_id,'siteurl'));
 
 	$url = str_replace( 'http://', "{$scheme}://", $url );
 
@@ -2053,7 +2053,7 @@
 			$scheme = ( is_ssl() ? 'https' : 'http' );
 	}
 
-	$url = 'http://' . $current_site->domain . $current_site->path;
+	$url = 'http://' . $current_site->domain . $current_site->site_path;
 
 	$url = str_replace( 'http://', "{$scheme}://", $url );
 
Index: wp-includes/ms-blogs.php
===================================================================
--- wp-includes/ms-blogs.php	(revision 13890)
+++ wp-includes/ms-blogs.php	(working copy)
@@ -27,10 +27,14 @@
 	global $current_site;
 
 	if ( is_subdomain_install() ) {
+		if(strpos($current_site->domain,$blogname)!==false)
+			return esc_url('http://'.$current_site->domain . $current_site->path);		
 		if ( $blogname == 'main' )
 			$blogname = 'www';
 		return esc_url( 'http://' . $blogname . '.' . $current_site->domain . $current_site->path );
 	} else {
+		if(strpos($current_site->path,$blogname)!==false)
+			return esc_url( 'http://' . $current_site->domain . $current_site->path);
 		return esc_url( 'http://' . $current_site->domain . $current_site->path . $blogname . '/' );
 	}
 }
Index: wp-includes/ms-default-constants.php
===================================================================
--- wp-includes/ms-default-constants.php	(revision 13890)
+++ wp-includes/ms-default-constants.php	(working copy)
@@ -46,7 +46,7 @@
 	 * @since 1.5.0
 	 */
 	if ( !defined( 'SITECOOKIEPATH' ) )
-		define( 'SITECOOKIEPATH', $current_site->path );
+		define( 'SITECOOKIEPATH', $current_site->site_path );
 
 	/**
 	 * @since 2.6.0
Index: wp-includes/ms-functions.php
===================================================================
--- wp-includes/ms-functions.php	(revision 13890)
+++ wp-includes/ms-functions.php	(working copy)
@@ -909,7 +909,7 @@
 
 // Install an empty blog.  wpdb should already be switched.
 function install_blog($blog_id, $blog_title = '') {
-	global $wpdb, $table_prefix, $wp_roles;
+	global $wpdb, $table_prefix, $wp_roles,$current_site;
 	$wpdb->suppress_errors();
 
 	// Cast for security
@@ -923,7 +923,9 @@
 	$wpdb->suppress_errors(false);
 
 	$url = get_blogaddress_by_id($blog_id);
-
+	$site_url=$url;
+	if($current_site->path!=$current_site->site_path)
+		$site_url=trim($url,'/').'/'.trim($current_site->site_path,'/').'/';
 	// Set everything up
 	make_db_current_silent();
 	populate_options();
@@ -931,9 +933,9 @@
 	$wp_roles->_init();
 
 	// fix url.
-	update_option('siteurl', $url);
+	update_option('siteurl', $site_url);
 	update_option('home', $url);
-	update_option('fileupload_url', $url . "files" );
+	update_option('fileupload_url', $site_url . "files" );
 	update_option('upload_path', "wp-content/blogs.dir/" . $blog_id . "/files");
 	update_option('blogname', stripslashes( $blog_title ) );
 	update_option('admin_email', '');
Index: wp-includes/ms-load.php
===================================================================
--- wp-includes/ms-load.php	(revision 13890)
+++ wp-includes/ms-load.php	(working copy)
@@ -96,11 +96,13 @@
  * @return object $current_site object
  */
 function wpmu_current_site() {
-	global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
+	global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain,$site_path;
 	if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
-		$current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
+		$site_path = defined('PATH_WP_INSTALL') ? PATH_WP_INSTALL:PATH_CURRENT_SITE;
+		$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;
+		$current_site->site_path=$site_path;
 		if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
 			$current_site->blog_id = BLOG_ID_CURRENT_SITE;
 		elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
@@ -126,10 +128,12 @@
 		$current_site = $sites[0];
 		wp_load_core_site_options( $current_site->id );
 		$path = $current_site->path;
+		$site_path = defined('PATH_WP_INSTALL') ? PATH_WP_INSTALL:$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 );
+		$current_site->site_path=$site_path;
 		wp_cache_set( 'current_site', $current_site, 'site-options' );
 		return $current_site;
 	}
@@ -149,7 +153,9 @@
 
 	if ( $current_site ) {
 		$path = $current_site->path;
+		$site_path = defined('PATH_WP_INSTALL') ? PATH_WP_INSTALL:$path;		
 		$current_site->cookie_domain = $cookie_domain;
+		$current_site->site_path=$site_path;
 		return $current_site;
 	}
 
@@ -158,6 +164,8 @@
 		$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;
+			$site_path = defined('PATH_WP_INSTALL') ? PATH_WP_INSTALL:$current_site->path;			
+			$current_site->site_path=$site_path;			
 			return $current_site;
 		}
 
@@ -166,6 +174,8 @@
 
 	if ( $current_site || defined( 'WP_INSTALLING' ) ) {
 		$path = '/';
+		$site_path = defined('PATH_WP_INSTALL') ? PATH_WP_INSTALL:$path;		
+		$current_site->site_path=$site_path;
 		return $current_site;
 	}
 
