When you upload a media file to WordPress, whether it’s an image, video, document, whatever, it creates its own attachment page, with its own URL, just like any other page or post.
These attachment pages are published by default, publicly accessible, visible without restriction, even if you haven’t attached the media to any post or page.
Look below, it is a page with everything, its sidebar, header, navigation links, etc.
And not only that, but they are pages that by default index in search engines, so if Google bots or other search engines crawl URLs on your website, although by default they are not shown in the WordPress default sitemap, they will find all those URLs and index them, with the problem that they are pages known as soft 404 or thin content, that is, with little content, barely relevant, in addition to increasing the amount of total URLs of your site, with URLs that contribute nothing to the SEO of your website.
Of course, then there is the fact that you probably don’t want there to be URLs indexed with images or videos that, for whatever reason, you may not want to appear in search engines and be seen by anyone, because I’m telling you that URLs are created even if the media are not attached to any publication.
Do we agree that this is a problem?
It is something that should be removed from WordPress, for its negative implications both SEO and security/privacy.
Disable links to attachments
One way is to disable links to attachments altogether.
Just add the following code to the functions.php file of the active theme (child) or to your customization plugin:
/* Disable links to attachments */
function cleanup_attachment_link( $link ) {
return;
}
add_filter( 'attachment_link', 'cleanup_attachment_link' );
Code language: PHP (php)
When you save your changes, links to attachments will be completely disabled, both in the media library screen and even if you choose to link an image to its attachment page.
Redirect attachment page to image file (with code)
A possible solution is to automatically create a permanent redirect from the attachment page to the file, this way no indexable page is created, only the image/video/document exists with its full server path.
As before, we will have to add a code, in this case this one:
/* Redirect attachment pages to the attachment file */
add_action( 'template_redirect', 'wphelp_redirect_attachment_file', 10 );
function wphelp_redirect_attachment_file() {
if( is_attachment() ) {
$url = wp_get_attachment_url( get_queried_object_id() );
wp_redirect( $url, 301 );
}
return;
}
Code language: PHP (php)
With this solution, we also avoid SEO penalties since no URLs are left with a 404 error but lead to an image with a permanent 301 redirect.
Redirect attachment page to the image file (with SEO plugins)
If you’re not comfortable with codes, no problem, because most of the current SEO plugins also use this technique of redirecting attachments to the file to avoid the indexing problem.
Let’s see where is the corresponding setting in the main SEO plugins…
Yoast SEO
In the Yoast SEO plugin it is a setting that comes active by default if you just installed it, but just in case, check if you have the redirect active in SEO → Search Appearance → Media.

Rank Math
This SEO plugin does something different, because in addition to not including any user-customizable settings, by default redirects the URL of the attachments to the URL of the entry in which the attachment is inserted.
And what happens in the case of media URLs not attached to any page or entry? Well, it redirects to the front page of the website.
I think it is not the most successful solution, but fortunately you can use a filter, which you must add, as always, to the functions.php file of the active theme or your various customizations plugin, which will change this behavior, to the rest of plugins, redirecting the URL of the attachment to the media.
The filter is this:
add_filter( 'rank_math/frontend/attachment/redirect_url', function( $redirect, $post ) { return $post->guid; }, 10, 2 );
Code language: PHP (php)
All in One SEO
In this veteran plugin, recently renewed, we are going to find all the possibilities of the two previous ones.
In the settings page located in All in One SEO → Search appearance → Media → Attachments, you will find a selector in which to choose whether….
- You do not redirect (Disabled).
- 301 redirect to the media (Attachment)
- You do 301 redirect to the post with the attachment (Attachment Parent).

Of course, to begin with, I think it’s a bad idea not to activate the redirect by default, because this default WordPress behavior is bad for SEO, and a SEO plugin should contemplate it and, by default, as Yoast does, create the redirect.
That said, I like that it offers options, because it is always good to educate the user by giving options.
And I also think it is appropriate that, in the case of leaving the redirect inactive, you can at least check so that the attachments are not indexed, as I point out in the screenshot above.
Which is better, redirect to the media or to the publication?
Finally, if you have doubts about the best method, it is always better to redirect to the media, to the file, because it works for all cases.
So my advice is to use either the code to redirect to the media or the Yoast SEO method, active by default, or activate the “Attachment” redirect of All in One SEO.
What you should not leave in any case is to activate the creation of attachment URLs without applying any measure, whatever it is.