I can’t figure out how to add canonical links that actually work with the Kirby multi language setup. Maybe someone can help?
Given:
- Each article / page has a urlKey
- Multi language is enabled
- Config with languages is properly setup
// Set languages
c::set('languages', array(
array(
'code' => 'en',
'name' => 'English',
'default' => true,
'locale' => 'en_US',
'url' => '/',
),
array(
'code' => 'de',
'name' => 'Deutsch',
'locale' => 'de_DE',
'url' => '/de',
),
I have a site with English and the German content:
English: site.com/
German: site.com/de/
The site contains one blog post on the English website and no blog posts on the German post:
site.com/blog/post/
site.com/blog/de/post/
Because of Kirby’s auto language fallback, the content from post is also used for post. This causes an issue with SEO ranking. A canonical link could fix it.
IE:
site.com/blog/de/post/ should have the canonical
<link rel="canonical" link="site.com/blog/post/".
I have tried to set it up as follows just for quick testing purposes:
<link rel="canonical" link="<?php echo $site->language()->url() ?>/<?php echo $page->urlKey() ?>"/>
Where urlKey get’s the CORRECT url because the post has it correctly set up as urlKey. There is no germanpost so it automatically grabs the urlKEY from post. Which is correct. BUT, the urlKey does not contain the language code. Therefore each canonical ends up without the original language code of the page request (not the content):
So:
<link rel="canonical" link="site.com/blog/post/".
becomes
<link rel="canonical" link="site.com/de/blog/post/".
Where de should not be inserted.
Same error the other way around.
Anyone knows how it fix this?