Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(); ?>
share|improve this question

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();
  }
}
share|improve this answer

If the page is reachable by a human, then an bot can and will index it, even though Google will follow a robots.txt file, not all search engines do.

You best bet is to just not have an attachment page, but you can also re-direct the attachment.php or image.php ( or anything really) by using something like this:

<?php wp_redirect(get_permalink($post->post_parent)); ?>

ps. You probably want to add a 301 to tell Google it has moved.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.