For some reason googlebot tried to index my media attachment pages and got redirected to media pages.
This seems to be a default wordpress behavior. To recreate it, upload an image called test.jpg to a wordpress installation. Create a post called my-post and add the media "test" to my-post using the wordpress editor. Then, if you request mydomain.com/my-post/test/ it will redirect to mydomain.com/test/
I have tried to add to functions.php the following:
add_action('template_redirect', 'disable_media_attachment_redirect');
function disable_media_attachment_redirect() {
if (is_attachment()) {
global $wp_query;
$wp_query->set_404();
status_header(404);
nocache_headers();
}}
When I request mydomain.com/my-post/test/ it first 301 redirects to mydomain.com/test/ and then returns a 404. I would like mydomain.com/my-post/test/ to return a 404 right from the beginning because googlebot visits mydomain.com/my-post/test/ and sees that it is a redirect, but never visits the mydomain.com/test/ which returns the 404 and shows googlebot that it should exclude this URL.