I have a bit of a problem/issue what is freaking me out a bit. I hope you can help me. If i do site:www.somesitename.com search in Google i see that Google is indexing my attachment pages. I want to redirect attachment URL’s to parent post and stop google from indexing them.

I have used different redirect plugins in hope that i can fix it myself but plugins don’t work. I get a error:”too many redirects occurred trying to open www.somesitename.com/?attachment_id=1982 “.

Do i need to change something in my attachment.php fail? Any idea what is causing this problem?

<?php
/**
 * The template for displaying attachments.
 *
 * @package WordPress
 * @subpackage 
 */

get_header(); ?>
</div>  <!-- /#header-bg -->
<div id="content-wrap" class="clearfix">
    <div id="content" role="main">
        <h1> </h1>
        <div id="main-content">

            <?php
            /* Run the loop to output the attachment.
             * If you want to overload this in a child theme then include a file
             * called loop-attachment.php and that will be used instead.
             */
            get_template_part( 'loop', 'attachment' );
            ?>

        </div> <!-- /#main-content -->
        <div id="side-content">
            <?php get_sidebar(); ?> 
        </div><!-- #side-content/ -->  
    </div>  <!-- #content/ -->
</div>  <!-- /#content-wrap -->

<?php get_footer(); ?>

2 Answers
2

Just add this to your functions.php or plugin.

add_action( 'template_redirect', 'attachment_redirect', 1 );
/**
 * Redirect any attachment page to their parent with 301 redirection
 */
function attachment_redirect() {
  global $post;
  if ( is_attachment() AND isset( $post->post_parent) AND is_numeric( $post->post_parent ) ) {
    wp_redirect( get_permalink( $post->post_parent ), 301 );
    exit();
  }
}

Leave a Reply

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