Index: wp-includes/rewrite.php
===================================================================
--- wp-includes/rewrite.php	(revision 17643)
+++ wp-includes/rewrite.php	(working copy)
@@ -782,7 +782,7 @@
 		foreach ( $posts as $id => $post ) {
 			// URL => page name
 			$uri = get_page_uri($id);
-			$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
+			$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ) );
 			if ( !empty($attachments) ) {
 				foreach ( $attachments as $attachment ) {
 					$attach_uri = get_page_uri($attachment->ID);
@@ -826,16 +826,19 @@
 		if ( is_array( $attachment_uris ) ) {
 			foreach ( $attachment_uris as $uri => $pagename ) {
 				$this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
-				$rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
+				$rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES, false));
 			}
 		}
 		if ( is_array( $uris ) ) {
 			foreach ( $uris as $uri => $pagename ) {
 				$this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
-				$rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
+				$rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES, false));
 			}
 		}
 
+		// page attachments catch-all
+		$rewrite_rules = array_merge( $rewrite_rules, array( '.+?/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]' ) );
+
 		return $rewrite_rules;
 	}
 
@@ -1389,11 +1392,8 @@
 					$sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...)
 					$sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...)
 					$sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx
-					//add an ? as we don't have to match that last slash, and finally a $ so we
-					//match to the end of the URL
 
-					//add another rule to match attachments in the explicit form:
-					//<permalink>/attachment/some-text
+					//add another rule to match attachments in the explicit form: <permalink>/attachment/some-text
 					$sub2 = $submatchbase . '/attachment/([^/]+)/';
 					$sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback
 					$sub2feed = $sub2 . $feedregex;    //feeds, <permalink>/attachment/feed/(atom|...)
@@ -1417,6 +1417,7 @@
 					}
 
 					//now we've finished with endpoints, finish off the $sub1 and $sub2 matches
+					//add a ? as we don't have to match that last slash, and finally a $ so we match to the end of the URL
 					$sub1 .= '?$';
 					$sub2 .= '?$';
 
@@ -1442,7 +1443,8 @@
 					//add regexes/queries for attachments, attachment trackbacks and so on
 					if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
 						$rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery, $sub1comment => $subcommentquery));
-					$rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite);
+					if ( ! $page || ! $this->use_verbose_page_rules ) //individual <permalink>/attachment/stuff only for non-verbose page or non-page rewrites
+						$rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite);
 				}
 			} //if($num_toks)
 			//add the rules for this dir to the accumulating $post_rewrite
