Index: wp-includes/link-template.php
===================================================================
--- wp-includes/link-template.php	(revision 13945)
+++ wp-includes/link-template.php	(working copy)
@@ -1837,7 +1837,7 @@
 	if ( empty($blog_id) || !is_multisite() )
 		$home = get_option('home');
 	else
-		$home = untrailingslashit(get_blogaddress_by_id($blog_id));
+		$home = get_blog_option($blog_id,'home');
 
 	$url = str_replace( 'http://', "$scheme://", $home );
 
@@ -1899,7 +1899,7 @@
 	if ( empty($blog_id) || !is_multisite() )
 		$url = get_option('siteurl');
 	else
-		$url = untrailingslashit(get_blogaddress_by_id($blog_id));
+		$url = get_blog_option($blog_id,'siteurl');
 
 	$url = str_replace( 'http://', "{$scheme}://", $url );
 
Index: wp-includes/ms-blogs.php
===================================================================
--- wp-includes/ms-blogs.php	(revision 13945)
+++ 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 . '/' );
 	}
 }
