0

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.

1 Answer 1

0

Yoast SEO (and probably other SEO plugins as well) handles that issue by redirecting to the image itself.

I'm not sure how they do it, but you can either go the quick route and install the plugin, or you try to look trough the code to decipher it for your own code.

Otherwise, this is maybe better handled by adding something to robots.txt?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.