I have three websites under single magento installation and would like to generate hreflang tags for each websites manually.

I used the code mentioned in this question

<?php foreach (Mage::app()->getWebsites() as $website) {
    foreach ($website->getGroups() as $group) {
        $stores = $group->getStores();
        foreach ($stores as $store) {
            $storeCode = substr(Mage::getStoreConfig('general/locale/code', $store->getId()),0,2);
            echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $store->getCurrentUrl(false) . '"/>' . "\n";

However, this only outputs hreflang available as storeview language. Also no coutntry is specified. I would like to use more hreflang tags as fi,de, en-ca etc.

How can I manually write hreflang tag?

--- UPDATE ---

For now, I could manually write hreflang by creating head.phtml for each websites. Then to add, for example,

<link rel="alternate" href="http://www.mywebsite.com<?php $urlString = Mage::helper('core/url')->getCurrentUrl(); $url = Mage::getSingleton('core/url')->parseUrl($urlString); $path = $url->getPath();echo $path ?>" hreflang="en-gb" />

This works. And so far Google Webmastertool is not giving any errors. However, this method obviously doesn't list all the available languages in my shop. But if I don't create each head.phtml and just have one head.phtml with the code above, it will add extra store code when you are on different website. Any good solution for this?

1 Answer 1


Try replacing

$storeCode = substr(Mage::getStoreConfig('general/locale/code', $store->getId()),0,2);

with this:

$storeCode = strtolower(str_replace('_', '_', Mage::getStoreConfig('general/locale/code', $store->getId())));
  • I tried your code. It adds country code. That's good. But still I don't want magneto to automatically output hreflang tag. I want to manually write for each hreflang version. I tried the code <link rel="alternate" href="example.com/ca<?php $urlString = Mage::helper('core/url')->getCurrentUrl(); $url = Mage::getSingleton('core/url')->parseUrl($urlString); $path = $url->getPath();echo $path ?>" hreflang="en-ca" />
    – user9839
    Mar 18, 2015 at 8:45
  • but then it adds extra store code. <link rel="alternate" href="example.com/ca/ca/sample-product.html" hreflang="en-ca" /> So I am not sure how to do this.
    – user9839
    Mar 18, 2015 at 8:45
  • I don;t know where that extra store code comes from. Do you have "use store codes in url" set to Yes in config?
    – Marius
    Mar 18, 2015 at 8:47
  • No. In this page magento.stackexchange.com/questions/12504/… some suggestions were given. I tried it also. Extra code doesn't appear. Also with the code posted in my question above. But still this is automatically generated. I want to write manually for each language. So that I can get results like <link rel="alternate" href="example.com/en/sample-product.html"; hreflang="en-ca" /> <link rel="alternate" href="example.com/eu/sample-product.html"; hreflang="en_gb" />
    – user9839
    Mar 18, 2015 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.