7 Ways to Redirect WordPress Attachment Pages

Every time you upload a media file to your website, WordPress will automatically generate a new “attachment” page for that file. In other words, there is an attachment page for each file upload that you have on your website. This page displays your embedded PDF file with a description if any on a blank page.

For example, if you upload a file called new-file.pdf. WordPress uses the permalink structure to create a new page with this  URL of https://preventdirectaccess.com/new-file/. In case you embed or attach this file into a WordPress page or post called “parent-page”, its attachment page URL will become https://preventdirectaccess.com/parent-page/new-file/

WordPress attachment pages are generally bad

In most cases, WordPress attachment pages do more harm than good as they contain little if no content all. Google and other search engines may index both your media files and their attachment pages which would cause a huge problem in terms of SEO. Google has always valued high-quality content and devalued low-quality content. And these WordPress attachment pages are often considered as duplicate content. So they actually can hurt your SEO efforts.

What’s more, attachment pages expose your private files and that users may be able to access your private files directly without having to log in or enter passwords.

In this article, we will show you 7 ways to handle these attachment pages.

Redirect to Attached Page or Post

The first option is to redirect the attachment pages to the files’ parent page or post that they belong to.

1. Use the All in One SEO Pack plugin

All In One SEO All In is a well-known WordPress plugin. They include an option under General Settings that allows you to redirect your WordPress attachment pages to pages or posts which the attachment is a part in 3 steps:

  1. Install and activate All in One SEO.
  2. Once activated, click on All in One SEO.
  3. Check the Redirect Attachments to Post Parent box and save.

2. Use Attachment Pages Redirect plugin

Attachment Pages Redirect plugin available for free on WordPress.org comes in handy if you don’t use any SEO plugins. It executes a redirect from the WordPress attachment page to the post that stores the attachment, or the homepage if the attachment does not belong to any pages or posts.

Simply install, activate the plugin and then just let it do the job. There is no plugin settings or any additional configurations needed.

3. Edit Attachment.php template

You can also create your own custom function to redirect WordPress attachment pages to either the homepage or the post parent.

Simply create a new file in your WordPress theme folder namely attachment.php, add this code snippet on top of the file:

<?php 
global $post;
if ( $post && $post->post_parent ) {
    wp_redirect(esc_url(get_permalink($post->post_parent)), 301);
    exit;
} else {
    wp_redirect(esc_url(home_url( '/' )), 301);
    exit;
}
?>

Now, refresh your attachment page it should redirect to the parent post or if it does not belong to any posts, it will redirect to your site’s homepage.

Redirect to the actual File URL

In some cases, you may want to redirect attachment pages to the actual file URL instead.

4. Use Yoast SEO plugin

Together with All in One SEO Pack, Yoast SEO had also come with an option to redirect attachment pages to post parent on version 7.0 and earlier. This option, however, was not always stable causing a lot of issues especially when an image was added to multiple parts of your site. Since then, the option is now updated to redirect the attachment pages to the actual file URLs.

To enable this feature, follow these simple steps:

  1. Install and activate Yoast SEO plugin
  2. Once activated, click on SEO
  3. Click on Search Appearance, and then, Media tab
  4. Enable the Redirect attachment URLs to the attachment itself? option

5. Edit Child Theme Templates

You can also create your own custom function to redirect WordPress attachment pages to the attachment itself with just one line of code.

Simply create a new file in your WordPress child theme folder namely attachment.php, then put this code below on top of the file.

<?php wp_redirect(wp_get_attachment_url(), 301); ?>

6. Remove Attachment Page URL

Although you have redirected the attachment page to its parent post or file URL, WordPress still provides the attachment page pretty URLs on the media library and media insertion dialog. You can remove this URL as well:

function remove_attachment_page_link( $link ) {
    return;
}
add_filter( 'attachment_link', 'remove_attachment_page_link' );

So now even if you select “Link to attachment page” when inserting your attachment file, the attachment file will be inserted without the link.

7. Protect File URL Against Direct Access

In some niches, attachment pages are actually useful for writers, designers, and photographers to show off their portfolios through galleries and sliders. In such cases, you should protect your private files against direct access instead. Prevent Direct Access Gold is the number one WordPress plugin that helps you protect your media files with ease.

Leave a Reply

Your email address will not be published. Required fields are marked *