Sunday, December 4, 2022
HomeMoney Making WayBlogging GuideStop Google Indexing WordPress Image Attachment Pages

Stop Google Indexing WordPress Image Attachment Pages

Are you worried about those strange search results that you came across recently upon searching “site:yoursitename” on google ? What makes you worried is when you click on those results , you end up opening only an image in a blank page without any actual content.

The other day i was going through and doing some on-page SEO clean-up of my site and I noticed multiple “image pages” indexed on Google. Whenever you add an image to your post it automatically attaches a link to the image that goes to a blank post (WordPress stores attached image data as posts in the db) with nothing but that image and maybe a title and caption (depending on the theme you use).

10 Signs That Indicate You’re Over-Optimizing Your Blog

The issue is that these pages are virtually pointless as they have no content. You are better off linking to your actual image itself if you want people to be able to click and see a large version or using a lightbox plugin for your posts. The attachment pages in WordPress are what’s considered “thin” content, so they hold no value and can actually harm your SEO efforts.

In this article I’ll show you different ways you can disable this main image attachment page on your WordPress site and redirect your page rank back to it’s primary article or your site homepage. And redirecting these attachment posts via 301 redirects is the best solution for both usability and SEO.

Redirect Image Attachment Pages With The Yoast SEO Plugin

The easier way is to redirect your attachment pages by using a plugin, and a free one at that. Here are the 3 easy steps:

  1. Install and setup Yoast SEO for WordPress
  2. Once setup, click on SEO > Advanced and click on the Permalinks tab
  3. Enable the option to “redirect attachment URLs to parent post URL” and save

Redirect Via Custom Function

Top 10 Blogging Mistakes to Avoid in 2020

You can also create your own custom function for redirecting your attachment pages to either the homepage or the post parent. This function should be placed in your functions.php file (preferably in a child theme if you are using a theme by another developer).

function myprefix_redirect_attachment_page() {
	if ( is_attachment() ) {
		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;
		}
	}
}
add_action( 'template_redirect', 'myprefix_redirect_attachment_page' );

If you notice the code will only redirect when is_attachment() returns true. This means it will redirect for any attachment type not just images, which is usually best. However, you can target only image attachments by adding some extra checks to see what the current attachment type is.

Redirect Via The image.php File

Your second option is to completely disable your attachment pages by adding a WordPress redirect directly added to the top of your image.php file.

  1. Create a new file called image.php in your child theme (always best to work with child theme’s when editing a theme) or if you are developing your own theme create an empty image.php file in your theme.
  2. Insert the code below in your image.php 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 when you refresh your image attachment page it should redirect to the original post where the image was uploaded or if it was uploaded directly in the media library it will redirect to your site’s homepage.

How to Install WordPress-Complete Guide

5 Crucial Steps to Improve Your Brand Online Presence

RELATED ARTICLES

Most Popular

Recent Comments