1. Home
  2. Knowledge Base
  3. SEO Suite
  4. How to Redirect Attachments to the Media URL with Rank Math

How to Redirect Attachments to the Media URL with Rank Math

In this knowledgebase article, we’re going to walk through the steps on how to redirect attachments to the media URL using Rank Math.

Why You Should Redirect WordPress Attachments?

The major reason for you to redirect the WordPress attachments is that it has a huge impact on your SEO. Some of the other reasons are:

  • It’s possible that the search engines might start bringing in more traffic to the attachment pages instead of your original post.
  • Most of the time, the attachment page seems to be out of context and a visitor may not be able to understand it. They will simply close the link and move away which thereby would result in an increased bounce rate.
  • It’s also quite possible that Google might index all of the image files that are available and treat them as duplicate content.

How to Redirect Attachments to the Media URL Using Rank Math?

By default, Rank Math offers to redirect attachments to the parent posts. However, you can use the below filter in the theme’s function.php in order to redirect the attachment URL to the actual media URL.

add_filter( 'rank_math/frontend/attachment/redirect_url', function( $redirect, $post ) { return $post->guid; }, 10, 2 );

Note that .jpg or .mov or .pdf file locations are not called attachments so the option will not redirect them. In fact, we do not recommend redirecting those actual media files because then you’ll not be able to view those images, download the PDF, or watch the video. Let’s look at some of the examples to understand in a better way.

Examples of Redirect Attachments

1 PDF File

For example, Notice how the attachment URL http://test.local/my-trip-to-new-york/newyork-2/, and the file URL http://test.local/wp-content/uploads/2021/02/newyork.pdf is different.

pdf Redirection

When you enable the redirect attachment option – the attachment URL will get redirected to the same post where it was added in. It will not affect the file URL and it will not redirect the .pdf file URL.

2 MP3 File

In the below example, the attachment URL and the file URL are different. On the same note, mp3 files will get redirected to the same post where it’s added and it will not affect the file URL, and neither it will redirect the .mp3 file.

MP3 File redirect

3 Image Attachment

In this example the attachment URL is http://test.local/about-page/ and the file URL is http://test.local/wp-content/uploads/2021/02/about-page.jpg and they both are different.

Image Attachment

The image file URL is not redirected.

We hope you’ve managed to redirect attachments to the media URL – and if you still have any questions, please free to reach out to our support team if you have any other questions.

Was this article helpful?

Still need help?

?

Submit Your Question

Please give us the details, our support team will get back to you.

Open Ticket

Related Articles