1

I have multiple store site in magento. i wanted to add hreflang for my stores and thanks to one of the post here i have managed to do so.

the code i wrote in my

app / design / frontend / mytheme / default / template / page / html / head.phtml

and the code it self is

    $website = Mage::app()->getWebsite()->getStores();
foreach ( $website as $store) {
    $lang = $store->getConfig('general/locale/code');
    echo '<link rel="alternate" href="' . preg_replace('/\?.*/', '', $store->getCurrentUrl()) . '" hreflang="' . $lang . '"/>' . "\n";
}

and the result i am getting on front end is

<link rel="alternate" href="http://www.mysite.co.uk/en/" hreflang="en_GB"/>
<link rel="alternate" href="http://www.mysite.co.uk/fr/" hreflang="fr_FR"/>
<link rel="alternate" href="http://www.mysite.co.uk/de/" hreflang="de_DE"/>
<link rel="alternate" href="http://www.mysite.co.uk/it/" hreflang="it_IT"/>

but the issue is, as you know hreflang="en_GB" is incorrect and google wont recognize it. the tag should be hreflang="en-gb" dash - instead of _ and all lower caps. how to achieve this? and 2nd is it important to have URL parameter /en/ for English site? cant this be without /en/? if so then how to achieve this? rest EU sites are fine to have parameters but English store should be with out it. Thanks

2 Answers 2

4

I'd recommend keeping the /en/ in the url, because it will help search engines index the correct language pages.

To convert the string en_GB to en-gb, you can use the following:

strtolower(str_replace('_','-',$lang));

Hope this helps!

EDIT:

In the example code shown in the question, please modify the following line:

$lang = $store->getConfig('general/locale/code');

into:

$lang = strtolower(str_replace('_','-',$store->getConfig('general/locale/code')));
2
  • Hi Milan, Thanks for your help, can you tell me where to add that string in my above code?
    – shafiq
    Commented Sep 23, 2015 at 9:40
  • I updated the answer with an example Commented Sep 23, 2015 at 14:01
-1

I Found the solution. this is the full code:

$website = Mage::app()->getWebsite()->getStores();
foreach ( $website as $store) {
    $lang = $store->getConfig('general/locale/code');
if (strtolower($lang) == 'en_gb'){
        $lang = 'en-gb'; 
    }
if (strtolower($lang) == 'fr_fr'){
        $lang = 'fr-fr';
    }
if (strtolower($lang) == 'de_de'){
        $lang = 'de-de'; 
    }
if (strtolower($lang) == 'it_it'){
        $lang = 'it-it';
    }
    echo '<link rel="alternate" href="' . preg_replace('/\?.*/', '', $store->getCurrentUrl()) . '" hreflang="' . $lang . '"/>' . "\n";
}

and i pasted this in file:

httpdocs / app / design / frontend / mytemplate / default / template / page / html / head.phtml

and now the results are:

<link rel="alternate" href="http://www.mysite.co.uk/en/" hreflang="en-gb"/>
<link rel="alternate" href="http://www.mysite.co.uk/fr/" hreflang="fr-fr"/>
<link rel="alternate" href="http://www.mysite.co.uk/de/" hreflang="de-de"/>
<link rel="alternate" href="http://www.mysite.co.uk/it/" hreflang="it-it"/>

Hope this will help some one.

1
  • 1
    This is not the best solution. If you ever add a new language you will have to edit the code again. What Milan Simek answered should fix it for life.
    – Marius
    Commented Sep 23, 2015 at 12:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.