Redirect Attachment Page to Post URL in WordPress

Each media folder upload to a post or page in WordPress is attached to that post or page. Though sharing of media files is possible across posts, one media file can be attached to one post at any point of time. WordPress treats each attachment file as an individual post and allows the file to be accessed with its own URL. Consider a case when you upload an image named “clipboard.png” to your “Hello World” post under the showcasing site “wordpress.webnots.com”. You can see the uploaded image URL and the link to URL in “Insert Media” screen as shown below:

WordPress Attachment URLs
WordPress Attachment URLs

URL of the image will be – http://wordpress.webnots.com/wp-content/uploads/2014/10/clipboard.png.

Link To field has four different options selectable form the drop-down:

  • Media File
  • Attachment Page
  • Custom URL
  • None

Attachment Link To Options in WordPress
Attachment Link To Options in WordPress

Media File – is the default option selecting which will result in the source image is being displayed when clicked on the image on the published post. In this case media file will be linked to the source URL – http://wordpress.webnots.com/wp-content/uploads/2014/10/clipboard.png.

Attachment Page – Choosing this will display the attachment page when clicked on the image. The URL of the attachment page will be – http://wordpress.webnots.com/hello-world/clipboard/.

Custom URL – Here you can set any redirection to the image.

None – will disable linking the image.

Now that Google will index the image “clipboard.png” and someone clicking on it in the search results will be taken either to media file or to attachment page unless you set custom URL in link to option. This is embarrassing situation for the visitor to see just the image or the attachment page and not the actual post content.

Related:  Creating Drag and Drop Header for Custom and Old Weebly Themes
WordPress Attachment Page View
WordPress Attachment Page View

Redirecting Attachment Page to Parent Post URL

Setting custom URL on each image is really not a possible task and the requirement is to set a site level redirection of attachment pages to parent post URLs. Fortunately many plugins are available for this purpose and “Yoast WordPress SEO” is one such a plugin. Install and activate this plugin and navigate to “SEO > Permalinks” section. Check the box “Redirect attachment URL’s to parent post URL” as shown in the below picture:

Redirect Attachment URL to Post URL
Redirect Attachment URL to Post URL

Save your changes and clicking on any media file attached to a post now will open the post instead of the image or attachment page.

Uploading a media files directly through “Media > Add New” path will not attach the file to any post and hence will be individually available in search engines as a file.

If the same image is linked across posts then the attached post will be the parent post and all other posts will not be considered for redirection.

Recommended Articles:

Leave a Comment