There are practically no scenarios where you would want your attachment page URLs to be kept available to the general public. This is particularly true for having those pages included in your sitemap.
Because if you do – search engines will crawl and index those pages, despite them having no content (other than the image), and in most cases; the pages will have janky titles generated from the image name itself.
Should you disable direct links to attachment pages?
So, one of the things that WordPress does when you upload new files to your Media Library is that it creates a new Permalink URL for those pages. This includes any images, video files, documents, and anything else you upload to your Media Library.
Let’s take, for example, my collection of WordPress tech blog themes.
If I go back to that post and open it in my WordPress editor – I can click on any of the included images and open it through Media Library. And this is what I will see:
As you can see, WordPress has created a separate permalink structure (page) for an individual image included in the post. And, unless I had disabled linking to attachment pages – this image could be accessed as a page by both my readers and search engines.
Imagine having 15 or so images per post, with a total of 100 posts on your blog. That would be well over 1,000+ attachment pages that provide no real value.
So, how do you fix this and disable attachment pages for individual files?
#1: Disabling Image Attachment Pages with a Plugin
One of the quickest ways to deal with the issue of attachment pages is to disable (redirect) them in the settings of your favorite SEO plugin. Mine is Yoast SEO, but I know other SEO plugins such as All In One SEO and others provide this feature, too.
If you go to Admin Dashboard -> Yoast SEO -> Search Appearance -> Media – you should see a page that looks like this:
You’ll want to select Yes for Redirect attachment URLs to the attachment itself? and that should do it. This is also an approach that I have highlighted in my How to fix 404 Error Pages in WordPress article.
If you’re not using one of the mentioned SEO plugins for whatever reason, you can try using this Disable Media Pages plugin.
This plugin provides the same functionality for disabling media attachment pages.
#2: Redirect Image Pages Without a Plugin
An alternative method for disabling/redirecting image attachment pages is by using a tiny code snippet that you can add to the image.php file in your theme’s root folder.
If the file doesn’t exist, you can create/upload it yourself.
As for the snippet – we will be using the wp_redirect() function.
<?php wp_redirect(get_permalink($post->post_parent)); ?>
First, wp_redirect() will fetch the permalink of the image, then grab the post-id and finally redirect to the parent page whenever someone tries to access the attachment page.
And that about sums it up. With just these 3 outlined methods – it should be plenty to help you disable image attachment page links without hurting your SEO or website structure.