Redirect loop for post attachments
-
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)); ?>
-
You can simply remove the PHP tags from my code and paste this inside the existing PHP tags of the file:
function noindex_attachment_pages() { if(is_attachment()) { echo '<meta name="robots" content="noindex, nofollow" />'; } } add_action('wp_head', 'noindex_attachment_pages');Ok option 1 was no sweet success I am afraid:)
Not sure If I will be able to do, alternative 2 though. It seems a bit difficult!
If I understand it correctly, I just copy the header.php, and name it header-attachments.php.
Then do i need to add:
function noindex_attachment_pages() {
if(is_attachment()) {
echo ‘<meta name=”robots” content=”noindex, nofollow” />’;
}
}
add_action(‘wp_head’, ‘noindex_attachment_pages’);This?
And then make an attachment.php, and then insert the same code again?
For option 2, completely ignore that code.
1. Download a copy of your template’s header.php file to your computer.
2. Rename that copied file header-attachments.php.
3. Open header-attachments.php in a text editor and add the meta tag inside the head section.
4. Save header-attachments.php.
5. Upload header-attachments.php to your theme directory.
6. If there is an attachment.php file in your theme directory, download it. Or, if there is not, download index.php to your computer and rename that copy attachment.php.
7. Open attachment.php in a text editor.
8. Find the PHP function get_header().
9. Change get_header() to get_header(‘attachments’). Make sure you do not accidentally delete or change anything else; just put ‘attachments’ (including the single quotes) inside those parentheses.
10. Save attachment.php
11. Upload attachment.php to your theme directory.To be clear, attachment.php is the name of the second file.
Perhaps a stupid question, but the head section. Is where you see <head> for the first time like this?
<head>
<meta charset=”<?php bloginfo( ‘charset’ ); ?>” />Exactly. Make it look like this:
<head>
<meta name=”robots” content=”noindex, nofollow” />
<meta charset=”<?php bloginfo( ‘charset’ ); ?>” />ok, ive all of that!
But when looking through the page source, I still cannot find the metatags!
Go to pastebin.com. Paste the code for header-attachments.php into the text box. Set the expiration time to 1 hour and the page exposure to unlisted. Submit. When it gives you the link, post that link here.
Ok. Here you go.
OK, everything there is right. Let’s do the same for attachment.php.
You’re missing the closing single quote in get_header(‘attachments’) so it is defaulting.
Fix that and test again.
Cant believe i missed that. Sorry!
But I have fixed it, but nothing has changed
If you are using a cache plugin, such as WP Super Cache or the like, either turn it off or at least purge the cache.
If not, post functions.php to pastebin in the same way.
I have now disabled w3 total cache. No changes
Here is the functions.php
The topic ‘Redirect loop for post attachments’ is closed to new replies.