• I am running the Twenty Eleven theme. By using Yoast his SEO Plugin I tried to redirect post attachments to their parent page. I did this because I did not want Google indexing my post attachments (images) which at the moment is the case when looking for my website in google.

    However, when clicking on the images themselves in the post, they do indeed redirect, but when I try to access the post attachment through google search or by putting the url in the address bar, I get stuck in a redirect loop.

    What can I do about this? I have recently lost my positions for keywords in the serps, and I am afraid this might have been causing it. Tons of image posts going round google, that were not even accessible.

    p.s.
    It also happens when I put this in the image.php

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

Viewing 15 replies - 1 through 15 (of 38 total)
  • If your object is to avoid indexing images, just edit your robots.txt file to indicate the wp-content/uploads directory should not be indexed.

    User-agent: *
    Disallow: /wp-content/uploads

    http://www.robotstxt.org/robotstxt.html

    Thread Starter Kroon10

    (@kroon10)

    The problem is that I have uploaded these images as post attachments. Now I have indeed tried to make them link to the actual image, but there still is this for example:

    http://europeanmovies.org/danish-movies/best-danish-movies/in-a-better-world_112983663482/

    or

    http://europeanmovies.org/?attachment_id=4301

    And they turn up in Google, sometimes (before i lost ranking, no clue where i am now) higher than the actual pages.

    Even though when you click on this image in the actual post, it will take you to the wp/content file

    OK, you mean you don’t want attachment pages indexed.

    Probably the smartest way to do that is to inject a meta noindex, nofollow tag into your attachment pages, via your theme’s functions.php file.

    <?php
    function noindex_attachment_pages() {
    if(is_attachment()) {
    ?>
    <meta name="robots" content="noindex, nofollow" />
    <?php
    }
    }
    add_action('wp_head', 'noindex_attachment_pages');
    ?>

    If that doesn’t work, an alternative is to do the following:

    1. Create a new header file for you theme, named header-attachments.php. It will be an exact copy of your default header, except it will contain the added meta noindex inside your <head> section:

    <head>
    <!-- other head tags here -->
    <meta name="robots" content="noindex, nofollow" />
    </head>

    2. Create an attachment.php template page.

    3. Call your noindex header as part of that page code, like this:

    <?php
    get_header('attachments');
    ?>
    <!-- page code for the loop here -->
    <?php
    get_sidebar();
    get_footer();
    ?>

    Thread Starter Kroon10

    (@kroon10)

    Thanks a lot Doug! I guess that would solve it. Just have to manually set images linking to actual image first then.

    Does it matter where i put that code in the functions.php file? (I am not very experienced with these things)

    The short answer is no, it does not matter where you put the code in your functions.php file.

    I like to put code at the very bottom. That way, I know the last thing shown was the last thing added to the file, and I don’t have to go hunting for it.

    Thread Starter Kroon10

    (@kroon10)

    How do I know if it works? Just have to wait until the website gets crawled again?

    If so, then I will be patient and if it does not work I will try your alternative!

    Thanks so much for your help!

    Thread Starter Kroon10

    (@kroon10)

    Parse error: syntax error, unexpected ‘<‘ in /home/europea/domains/europeanmovies.org/public_html/wp-content/themes/twentyeleven/functions.php on line 595

    It immediately gives me this message after inserting your code

    Make the change. Go to an attachment page. View the page source (that’s usually an option if you right-click on the page, or you can save the page to your computer and look at the page source in a text editor).

    Look at the head section of the page. Someplace in there, you should see the meta tag. If not, it didn’t work, try option 2. If you see it, then sweet, sweet success.

    <?php
    function noindex_attachment_pages() {
    if(is_attachment()) {
    echo '<meta name="robots" content="noindex, nofollow" />';
    }
    }
    add_action('wp_head', 'noindex_attachment_pages');
    ?>

    Make sure you don’t paste this in the middle of anything else.

    Or use the second option if you can’t get this to work.

    Thread Starter Kroon10

    (@kroon10)

    Hmm, after that I actually keep getting the error as soon as I try to access my wp. Cannot even change it now. Any ideas?

    Thread Starter Kroon10

    (@kroon10)

    As a matter of fact, I cannot access anything now. I put it at the end like you told me.

    You pasted the code inside the opening and closing PHP tags. That’s why you got the error.

    If you edited the file on your server, using the wordpress theme editor, then you need to FTP to your site, download functions.php from wp-content/themes/yourthemename (where yourthemename is the name of your theme). Then, open functions.php in a text editor (Notepad or the like), remove the code, and FTP functions.php back to overwrite the file.

    If you edited the file offline, just remove the code and put it back.

    I should have realized you may not understand where to paste this code, so you have my apologies.

    Alternatively, if you are using a stock theme or have a copy of your theme on your computer, you can just upload the default functions.php file from that to your site.

    Again, when you post in this code, make absolutely sure it is not inside of anything else. As in, go to the very bottom of the file, hit a whole pile of newlines, and paste at the end.

    Thread Starter Kroon10

    (@kroon10)

    No apologies needed!

    I have already recovered it by going to the FTP, so tahts all good.

    But I had put it in at the end? How can I keep it outside the closing PHP Tags?

Viewing 15 replies - 1 through 15 (of 38 total)
  • The topic ‘Redirect loop for post attachments’ is closed to new replies.