What is the precendence of item types within WordPress

The precedence is determined by the order of the rewrite rules:

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/class-wp.php#L196

These map the structure to a query, which is then fed to WP_Query, which has its own defaults. I don’t see why you would be landing onto the media post, the post_id does not match, there should not be any collision in the first place. This does not appear to be undefined behavior, rather a bug in the rewrite rules, caused by some plugins or the theme etc. I assume that there’s a rewrite rule at the top which makes /pagename/.*/ fetch media post types.

Dump the rules (var_dump( get_rewrite_rules() )) and see if anything makes sense.

Further reading: