How to redirect attachment pages in WordPress

To redirect attachment pages in WordPress 6.4 or later, set the wp_attachment_pages_enabled option to 0. WordPress then sends every attachment page to the media file itself instead of showing a near-empty page. New installs since 6.4 have this off by default, but sites created earlier keep attachment pages on until you change it.

An attachment page is the page WordPress creates for every file you upload, at an address like /my-post/photo-name/ or /?attachment_id=123. It shows the image and not much else, so each one is a thin page Google can index. On a site with thousands of uploads, that is thousands of pages with no value.

Step by step

1. Check whether your site has attachment pages

Open Media → Library, click an image and look for a View attachment page link in the details panel. Click it. If a page loads showing the image inside your theme, attachment pages are on. If the browser goes straight to the file, they are already off.

2. Check whether Google indexed them

In Search Console, open Indexing → Pages and filter the indexed pages by URL for attachment_id, or look for URLs that end in image names under your posts. Note any that get clicks in the Performance report. Those are the ones worth redirecting somewhere specific.

3. Turn attachment pages off with WP-CLI

Run this from the site root:

bash
wp option update wp_attachment_pages_enabled 0

Without WP-CLI, open /wp-admin/options.php while logged in as an administrator, find wp_attachment_pages_enabled, change the value to 0 and click Save Changes at the bottom. That screen lists every option, so change only this one.

4. Or redirect to the parent post with code

Sending a visitor to a bare image file is fine for bots, less so for people who clicked a search result. To send attachment pages to the post the file was uploaded to, and to the file only when there is no parent, add this to a child theme or snippets plugin:

php
add_action( 'template_redirect', function () {
    if ( ! is_attachment() ) {
        return;
    }
    $id     = get_queried_object_id();
    $parent = wp_get_post_parent_id( $id );
    $target = $parent ? get_permalink( $parent ) : wp_get_attachment_url( $id );
    wp_safe_redirect( $target, 301 );
    exit;
} );

5. Redirect the few that earn traffic

For individual attachment URLs with clicks or backlinks, a specific destination beats a generic one. Add a rule in Hydrogen SEO → Redirections: the attachment path as From URL, the most relevant post or gallery as To URL, and 301 - Permanent Redirect. See how to set up 301 redirects.

6. Stop linking to attachment pages

In the block editor, an Image block's link setting can point to the Attachment page. Older posts made in the classic editor often used "Link to: Attachment page" too. Change these links to the media file or to nothing. Search your content for attachment_id to find the query-string form.

7. Keep them out of your sitemap

Open Hydrogen SEO → Sitemap and check the Post Types card. If a media or attachment type is listed and switched on, switch it off and click Save Changes. Image files still reach Google through the image entries attached to each post, controlled by Include Images in Sitemap.

Check it worked

Request an old attachment URL and look at the response:

bash
curl -sI "https://example.com/?attachment_id=123" | grep -iE "^(HTTP|location)"

You should see a redirect with a location header pointing at the file or parent post. Over the following weeks, the attachment URLs leave the indexed list in Search Console and appear under Page with redirect.

Common mistakes

  • Redirecting every attachment to the homepage. Google treats that as a soft 404, and visitors lose the image they came for.
  • Deleting the media files to get rid of the pages. That breaks the images in your posts.
  • Noindexing attachments but leaving them linked from every gallery. Redirecting is cleaner, because the page stops existing for visitors too.

Common questions

Are WordPress attachment pages bad for SEO?

They are thin pages that add little a searcher wants, and on large media libraries they can crowd the index. Most sites are better off without them.

Does turning off attachment pages affect images in my posts?

No. Images keep loading from their file URLs. Only the separate HTML page for each attachment goes away.

My site was created after WordPress 6.4. Do I need to do anything?

Probably not. New installs since 6.4 have attachment pages off by default. Check one image's attachment link to confirm.