Index: wp-includes/query.php
===================================================================
--- wp-includes/query.php	(revision 7443)
+++ wp-includes/query.php	(working copy)
@@ -998,7 +998,7 @@
 		}
 
 		// Category stuff for nice URLs
-		if ( '' != $q['category_name'] ) {
+		if ( '' != $q['category_name'] && !$this->is_singular ) {
 			$reqcat = get_category_by_path($q['category_name']);
 			$q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
 			$cat_paths = '/' . trim($q['category_name'], '/');
Index: wp-includes/link-template.php
===================================================================
--- wp-includes/link-template.php	(revision 7443)
+++ wp-includes/link-template.php	(working copy)
@@ -179,7 +179,7 @@
 			$parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
 		else
 			$parentlink = get_permalink( $object->post_parent );
-		if ( ctype_digit($object->post_name) )
+		if ( ctype_digit($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
 			$name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
 		else
 			$name = $object->post_name;

