Magento – Magento hreflang tags: How to get URL Key per Store View

hreflangmagento-1.9store-viewurl-key

I have been trying to add hreflang tags to a multi-language project following the answers to this question. In my setup there is one website, one store and there are three store views (English, German and Dutch).

The code that I'm currently using in

[my Magento install dir]/app/design/frontend/MYPACKAGE/MYTHEME/template/page/html/head.phtml

is the following

<?php foreach (Mage::app()->getWebsites() as $website) {
    foreach ($website->getGroups() as $group) {
        $stores = $group->getStores();
        foreach ($stores as $store) {
            $storeCode = $store->getCode();
            echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $store->getCurrentUrl(false) . '"/>' . "\n";
        }
    }
}
?>

This outputs the hreflang tags like this

<link rel="alternate" hreflang="en" href="domain.com/en/bags.html">
<link rel="alternate" hreflang="de" href="domain.com/de/bags.html">
<link rel="alternate" hreflang="nl" href="domain.com/nl/bags.html">

At first glance this looks perfect, however there is this one problem. The store is using different URL Keys per language (store view). To improve SEO, the URL Keys are translated to match the language of the store view. For store views other than the one that is selected, the current hreflang tags point to URLs that do not exist (resulting in 404 Page not Found errors).

So the hreflang tags that I am looking for should look like this

<link rel="alternate" hreflang="en" href="domain.com/en/bags.html">
<link rel="alternate" hreflang="de" href="domain.com/de/taschen.html">
<link rel="alternate" hreflang="nl" href="domain.com/nl/tassen.html">

How can I get the hreflang tags to match the proper URL Key for each store view?

UPDATE July 16, 2015

Even though I have not accepted Marius his answer (because I did not manage to get his second solution to work for me) I used his first approach and created 301 redirects for all pages in different languages.

This seemed to work out just fine but last week Google started sending out error messages to users for incorrect implementation on Hreflang through Google Search Console.

In the Search Console it says that Google has found URLs for my 'en' site and alternate URLs in 'nl' that do not have return tags. A screen shot of the console is below.

enter image description here

The Alternate 'en' URLs that Google is listing do appear in the <head> section of my site, but I have set 301 redirects to the 'nl' URLs in the Magento Admin Panel.

For some reason Google handles this as incorrect implementation of hreflang tags.

Best Answer

I think the simplest solution here will be to create 301 redirects for your pages in different languages in the url rewrites management admin section.
Something like this:

For DE store view redirect from bags.html to taschen.html.
For EN store view redirect from taschen.html to bags.html.

Do the same for all the other combinations.
this way, when you switch from domain.com/en/bags.html to DE you it will points to a page that does not exist domain.com/de/bags.html but that will do a 301 redirect to domain.com/de/taschen.html.
I know that there are a lot of redirects to create, but I can recommend you this extension. All you need to do is to provide the url keys for each store view and it will create automatically the redirects.

But if you don't like this approach, you may need to rewrite the Mage_Core_Model_Store::getCurrentUrl method to look in the url_rewrites table and get the url for each language. But this may cause performance issues and it is not a bullet proof solution, because it will not work for cms pages or other custom page.

I don't have a full working solution for this, but I can give you some pointers.
Look in the core_url_rewrites table for a target path matching the $requestString var in the method mentioned above for the store that should generate the url and append to the target path value the query string.

Related Topic