The Moz Q&A Community

Hey friend! Have fun exploring Q&A, but in order to ask your own
questions, comment, or give thumbs up, you need to be logged in to your
Moz Pro account.
You can also earn access by receiving 500
MozPoints
from participating in YouMoz and the Moz Blog!

rel="canonical" again

I should rel="canonical" my 2 languages website /en urls to the original version without /en. Can I do this from the header.php? Should I rel="canonical" each /en page (eg. en/contatti, en/pagina) separately or can I do all from the general <head> before the website title?

11 Responses

I don't want to post the same answer as I did to your previous question but perhaps there was further clarification that you needed, that I missed!

Put a conditional in the header. Since you are using a wordpress platform you can't go in and manually edit each pages canonical anyway. Using the page if function and a variable you would be able to assign each one it's own rel= from a central head file anyway.

In an ideal situation you'd do each page manually but because of your CMS you need to do a work around

Just to add in here and simplify the process, Wordpress has a built in function to return the current post/page URL. Make use of 'get_permalink()', with some simply string manipulation you would be able to output the correct canonical tag to your page.

Edit: My PHP is a little rusty at times, but the following should sort you out:

<?php //Check if the page you're on is a single post. If so run below.if ( is_single() ) {$url = get_permalink();$canonical = str_replace('/en', '', $url);

echo '<link rel="canonical" href="'.$canonical.'"/>';};?>

As mentioned above, put this into your header.php file (in the template directory), where you would like the canonical tag to appear.

1. Do not add a canonical sitewide tag - here is a case study on why http://moz.com/blog/catastrophic-canonicalization Long story short - he deindexed 57% of his website.

2. You could 301 redirect all the pages, instead of adding a rel canonical. If your /en version is a duplicate of the original version, then you could simply add a code that redirects each page to the relevant version, like this: RedirectMatch 301 ^/en/(.*)$ http://www.yourwebsite.en/$1

You can use that solution in the case where you have a website called www.mywebsite.com that has a www.mywebsite.com/en version for a lot of links if not all, and those are the ones indexed in Google. You just add that code into htaccess. So just replace mywebsite with your website.

Also canonical sitewide is problematic unless you add a customized conditional at the PHP level. He has a wordpress site and can't edit the raw HTML of every page so he needs to have a PHP string at the global level which changes based on page variables.

This is a response to both questions. Rel = canonical will give in this case the English page the authority, that should be the page that ranks well.

The main issue here is if the whole page is translated don't use a canonical tag, if the content stays in English and the Navigation/footer is changed use canonical tag to the English page as this would verge on duplicate content.

Thanks for reply. I have original webpages in Italian. Translated webpages are in English. I use a plugin for wordpress, that allows me to translate the whole page (title, etc.) except the url, which will only be different because of the /en before the original page name (in italian, eg /en/contatti)

Widgets, footers etc. are still in Italian, even with translated pages.

I also thought about changing permalinks to be %postname%, so that url may adapt to title (? I think). But Im afraid this website wide urls change will affect my current rankings.

As I see it, you don't need to use a rel="Canonical" because your pages are not duplicates. You have content in italian and translated content in English.

The only thing you need to do is add a rel="alternate" hreflang="x"

This is what Google recommends in case your website is fully translated.

Some example scenarios where rel="alternate" hreflang="x" is recommended: For example, you have both German and English versions of each page. Here is how you implement it: https://support.google.com/webmasters/answer/189077?hl=en

2. HTTP header. If you publish non-HTML files (like PDFs), you can use an HTTP header to indicate a different language version of a URL: Link: <http://www.yourwebsite.eg/en>; rel="alternate"; hreflang="en".

Thank you very much, I will better study the link you sent, and try to implement on my website. Footers etc. are not translated, so they remain in the original language.. But while reading, I think the solution can fit to my problem.

Hey friend! Have fun exploring Q&A, but in order to ask your own
questions, comment, or give thumbs up, you need to be logged in to your
Moz Pro account.
You can also earn access by receiving 500
MozPoints
from participating in YouMoz and the Moz Blog!
Learn more.