How to WordPress-Disable or Remove image attachment page URLs

How to WordPress: Disable or Remove image attachment page URLs

In WordPress image attachment page URLs are generated automatically and image attachment URLs will be indexed as a separate URL and if image attachment pages are indexed in google search, there will be an duplicate content issue as the image URL will be indexed as a page URL and generates individual pages for images. (image URL will be indexed as attachment URL), if a user clicks on it then he will be taken to image URL (attachment) and in URL you can see image/attachment/imagename.jpg and this is duplication in WordPress and search engines like google or Bing or yahoo will be considered as duplicate and put a penalty on your website for this reasons, so its always recommended to avoid duplication. So, lets see in detail how to disable or remove WordPress image attachment page URLs below.

What Happens if Image Attachment page URLs are indexed in Google?

If image attachment pages are indexed then that’s duplication (URL which is indexed should have content on page and if image attachment URL will not contain content it’s a image attachment and should be mapped and redirected to original content URL)  and you need to redirect images page or post URLs to what content they are on (article URL). (redirect image to page or post).

If you are using Yoast SEO plugin in WordPress then its pretty easy to remove just you need to turn on or turn off options available to remove or disable image attachment URLs in WordPress.

If you are not using Yoast SEO plugin then you need to add a bit of code in functions. Php file to remove or disable image attachment page URLs in WordPress from google.

Advertisement

How to Disable or remove image attachment page URLs in WordPress:

Using Yoast SEO Plugin -Disable or remove image attachment URLs in WordPress from google

Step 1: Login to your WordPress admin dashboard.

Step 2: Navigate to Yoast SEO Plugin->SEO->Advanced.

Note: If advanced settings is not enabled, then you need to enable it from Feature settings tab.

Step 3: In Advanced settings->Click on Permalinks tab.

Step 4: Now, enable the option Redirect attachment URLs to parent post url.

Step 5: Enable and save settings.

That’s it, this is how you remove or disable image attachment urls in WordPress using Yoast SEO plugin.

What happens if I disable or remove image attachment page URLs in WordPress using Yoast?

Once you enable this option all the image attachment page URLs will be redirected to the original post URL they were attached to and avoid duplication issue.

Remove or Disable image attachment URLs in WordPress without Yoast SEO plugin

If you are not using Yoast SEO plugin in WordPress then you need to add a code by adding a filter and using rewrite rules of WordPress to remove or redirect image attachment urls.

function cleanup_default_rewrite_rules( $rules ) {
    foreach ( $rules as $regex => $query ) {
        if ( strpos( $regex, 'attachment' ) || strpos( $query, 'attachment' ) ) {
            unset( $rules[ $regex ] );
        }
    }

    return $rules;
}
add_filter( 'rewrite_rules_array', 'cleanup_default_rewrite_rules' );

Once you add this code in your functions. Php file then you also need to open and save permalinks options once, so that the permalink gets refreshed.

Why to remove or disable image attachment URLs in WordPress?

By default WordPress will generate individual url for image and will be indexed which is not good for seo and to avoid duplication you need to redirect or remove or disable image attachment URLs in WordPress.

How to remove or redirect image attachment urls in WordPress?

You can create a new filter in WordPress and add it it to your functions. Php file or if you are using Yoast seo plugin then go ahead and enable redirect image to its it original content page, which will avoid duplication.

Does redirecting image attachment page URL will avoid duplication?

Yes! If you are redirecting image attachment page to its original parent url (In which image is inserted) then this will avoid duplication as WordPress generates individual url for image as attachment.