Attachment Page Redirects

Send the page WordPress makes for every upload, with a 301, to the post the file belongs to, or to the file when there is no public post.

4 min read
On this page
  1. Does your site have attachment pages?
  2. Why redirect them
  3. Switch it on
  4. Where each attachment page goes
  5. What it does not change
  6. Check it worked
  7. For developers
  8. FAQ
  9. Will this break images on my site?
  10. Can I go back?
  11. Should the redirect go to the file or the post?
  12. Do I still need this on a new site?
  13. Related reading

WordPress gives every file you upload a page of its own, called an attachment page: your theme's layout wrapped around one image or a download link, with the file's title and little else. Redirect Attachment Pages, added in Hydrogen SEO 0.0.28, sends each of those pages with a 301 redirect to the post the file was uploaded to, or to the file itself when there is no suitable post. It is off by default and free.

Does your site have attachment pages?

WordPress 6.4 changed the default. On sites installed with 6.4 or later, attachment pages are switched off and a request for one is sent to the file. Sites created before 6.4 keep them, and upgrading WordPress does not change that, so a blog that has been running for a few years usually still serves one page per upload.

To check, open Media → Library, click an image and follow its View attachment page link. If a page opens with the image inside your theme, the site has attachment pages. If the browser shows the bare file, WordPress is already redirecting them.

Attachment pages live at addresses such as /my-post/photo-name/, /photo-name/ or /?attachment_id=123, depending on your permalink settings and whether the file is attached to a post.

Why redirect them

An attachment page has almost no text, so to a search engine it is a thin page, and a site with a few thousand uploads has a few thousand of them. Visitors who land on one from an image search or an old gallery link see a picture without the article it belonged to. Sending them to the post puts the image back in its context, and the 301 tells search engines that the post is where the attachment page's links and signals should go.

Switch it on

  1. In wp-admin, open Hydrogen SEO → General (in the Settings group).
  2. In the Permalinks section, turn on Redirect Attachment Pages.

The General screen saves a couple of seconds after the change, or straight away with Save. The redirect works from the next request.

Where each attachment page goes

The file is attached to…The attachment page redirects (301) to…
A published post or page that anyone can view, without a passwordThat post or page
A draft, scheduled or password-protected postThe file
A private postThat post, for a signed-in user who can read it. Other visitors get the 404 WordPress already shows for these.
A post of a type that has no public pagesThe file
Nothing (uploaded straight to the Media Library)The file

"Attached to" means the post the file was first uploaded to. WordPress records that once, in the Uploaded to column of the Media Library list view. Inserting the same image into a second post later does not change it, so a reused image's attachment page goes to the post where it was first uploaded.

Files are never sent to a post that visitors cannot read. A draft or password-protected parent would show a 404 or a password form, so the file is the better answer.

If your media is served from a CDN or an offload service, the file address is whatever WordPress reports for the file, including that host.

What it does not change

  • Images and file links inside your posts. They point at the files themselves, which keep their addresses.
  • The Media Library. Files, their titles, alt text and captions stay as they are, and nothing is deleted.
  • Query strings. Tracking parameters such as ?utm_source= are carried over to the address the visitor lands on.
  • Previews, embeds and form posts. Only ordinary page visits are redirected; a post preview, an embedded attachment and a form submitted to the address are left alone, as WordPress does with its own redirect.
  • Redirects you made yourself. A rule in the Redirection Manager for an attachment page's address takes priority, so you can send a particular attachment page somewhere more specific, such as a gallery page.

On a site where WordPress already sends attachment pages to the file (any site installed with 6.4 or later), switching this on sends them to their post instead, where there is one.

Check it worked

Request an attachment page and look at the response headers:

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

You should see 301 and a location pointing at the parent post or the file. In Search Console, the old attachment URLs move to Page with redirect in the Pages report as Google recrawls them, which can take weeks on a large site.

For developers

The hydrogen_seo_attachment_redirect_url filter receives the target URL and the attachment's WP_Post object. Return a different URL to change where an attachment page goes, or an empty string to keep that attachment page.

php
add_filter( 'hydrogen_seo_attachment_redirect_url', function ( $target, $attachment ) {
    // Keep attachment pages for PDFs.
    if ( 'application/pdf' === $attachment->post_mime_type ) {
        return '';
    }
    return $target;
}, 10, 2 );

FAQ

Will this break images on my site?

No. Only the separate page WordPress builds for each file is redirected. Image and file URLs are untouched.

Can I go back?

Yes. Switch the setting off and, from the next request, WordPress handles attachment pages as it did before: older sites show them again, and sites installed with 6.4 or later send them to the file. Browsers that followed a 301 may remember it for a while, so test in a private window.

Should the redirect go to the file or the post?

For a visitor, the post is usually better: they get the image with the article around it. The file is used only when there is no public post to send them to. Thinking it through for your own site is covered in where should attachment pages redirect?.

Do I still need this on a new site?

Usually not, since WordPress already redirects attachment pages to the file on sites installed with 6.4 or later. Switch it on if you would rather visitors land on the post.

Was this page helpful?
Still stuck? Contact support