Magento rel alternate hreflang

This code implements the hreflang alternate tag wich is perfect if you have a magento multistore with different languages and want to push your store in Google.

I looked for a extension but i didn’t want to spend any money on it. Another reason why i didn’t took a magento plugin was because i have a multistore with 3 languages and 5 storeviews.

storeviews magento multistore

So i programmed a little hreflang alternate generator for all cases.

It’s perfect for a Magento multistore with multiple magento websites and multiple stores. It’s working with both, categories and products.

My hreflang extension features

  • It works with different domains
  • It detects products and categories
  • It detects if a product is set to invisible in a storeview
  • It detects if a category is active in a storeview
  • It detects the homepage
  • It does not work with CMS-pages, cart, wishlist and so on, but that’s okay because Google doesn’t care about that. If you have CMS pages, you can use static blocks and assign them to a category

Hreflang alternate tag for magento products

The language tags should look like that.

<link rel="alternate" hreflang="de-DE" href="" />
<link rel="alternate" hreflang="de-AT" href="" />
<link rel="alternate" hreflang="de-CH" href="" />
<link rel="alternate" hreflang="en" href="" />
<link rel="alternate" hreflang="nl" href="" />

Implement hreflang alternate in Magento

Just copy the following php code and paste it into that file:

  • app/design/frontend/default/THEME/template/page/html/head.php

After that change the store id’s in the code and upload the file.

 * Generate hreflang alternate tags for all languages
 * @autor: Andreas Grundner
function getAlternateUrl($iStoreCode) {

	$store = Mage::app()->getStore();
	$prod = Mage::registry('current_product');
	$categ = Mage::registry('current_category');
	$store_url   = Mage::app()->getStore($iStoreCode)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
    $categId = $categ ? $categ->getId() : null;

	# product
	if($prod) {
		$iProduct_id = $prod->getId();

		 	check if product is visible
		if (Mage::getModel('catalog/product')->setStoreId($iStoreCode)->load($iProduct_id)->getVisibility() != 1) {

			$sProductStoreUrl = Mage::getModel('catalog/product')->setStoreId($iStoreCode)->load($iProduct_id)->getProductUrl();

			if (strpos($sProductStoreUrl, '?')) {
				if($sOutput = substr($sProductStoreUrl, 0, strpos($sProductStoreUrl, "?"))) {
					return $sOutput;
				return $sProductStoreUrl;
		return false;
	# category
	elseif($categ) {

		$categoryId  = $categ->getId();
		$oCat = Mage::getModel('catalog/category')->setStoreId($iStoreCode)->load($categoryId);
		if($oCat->getIsActive()) {
			$aCaturlkey = parse_url($oCat->getUrl());
			return rtrim($store_url, "/") . $aCaturlkey["path"];
			return false;

	elseif(Mage::getBlockSingleton('page/html_header')->getIsHomePage()) {
	    return rtrim($store_url, "/");
	else {
		return false;

# Fill in the store_id from the database table core_store
	echo '<link rel="alternate" hreflang="de-DE" href="' . getAlternateUrl(1) . '/" />' . "\n";

	echo '<link rel="alternate" hreflang="de-AT" href="' . getAlternateUrl(3) . '/" />'  . "\n";

	echo '<link rel="alternate" hreflang="de-CH" href="' . getAlternateUrl(4) . '/" />'  . "\n";

	echo '<link rel="alternate" hreflang="en" href="' . getAlternateUrl(9) . '/" />'  . "\n";

	echo '<link rel="alternate" hreflang="nl" href="' . getAlternateUrl(8) . '/" />'  . "\n";

Sei sozial und teile

Über den Autor Andreas Grundner

Ich arbeite als Wordpress-, Joomla-, Typo3-, Magento-Integrator, Rootserver-Administrator und Webanwendungsprogrammierer. Meine Verpflichtung gilt dem Datenschutz, meine Leidenschaft dem Finden und Aufzeigen von Sicherheitslücken in Webanwendungen.

2 Reaktionen zu Magento rel alternate hreflang

  1. Alex
    09.08.2019 at 14:16 · Antworten

    This is a vrey good script for alternate hreflang for Magento 1.9

    To make it complete I have added the CMS pages.
    In order for this to work you must have already set up the rewrite urls for each CMS page language in Catalog->URL rewrite management.

    After line 53, just before ( elseif(Mage::getBlockSingleton(‚page/html_header‘)->getIsHomePage()) { ), paste the following code:

    # CMS page
    elseif (Mage::app()->getFrontController()->getRequest()->getRouteName() == ‚cms‘ && !Mage::getBlockSingleton(‚page/html_header‘)->getIsHomePage()) {
    $store_id = Mage::app()->getStore()->getId();
    $path = Mage::getSingleton(‚cms/page‘)->getIdentifier();
    $rewrite = Mage::getModel(‚core/url_rewrite‘)->setStoreId($iStoreCode)->loadByRequestPath($path);
    $targeturl = $rewrite->getTargetPath();
    if ($iStoreCode == $store_id) {
    return $store_url . $path;
    } else {
    return $store_url . $targeturl;

  2. A
    09.08.2017 at 11:17 · Antworten

    Very nice script!

    To make it perfect…How to add x-default..?

Meinung sagen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.