Hreflang Tags for Magento 2

Add hreflang tags to Magento 2 using extensions or custom implementation. Store view mapping and multi-store configuration.

Magento 2 doesn't generate hreflang tags out of the box. You need to add them via an extension or custom code -- but the underlying structure Magento uses makes hreflang implementation fairly straightforward once you understand it.

How Magento Handles Multiple Languages

Magento 2 uses store views to manage language variants. A typical setup looks like this:

  • One Magento installation
  • One or more websites
  • Multiple store views per website, each configured for a language (English, French, German, etc.)

Each store view has its own base URL, which is what you'll use in your hreflang annotations. The relationship between store views and hreflang alternates is one-to-one: each store view maps to one hreflang locale.

Option 1: Use a Hreflang Extension

The quickest path. Several extensions handle hreflang generation for Magento 2:

  • Amasty Store Switcher -- includes hreflang generation with store view mapping
  • Magefan Multi-Language Store -- dedicated localization tool with hreflang support
  • Mirasvit SEO -- broader SEO suite that includes hreflang tags

These extensions typically read your store view configuration, map each view to a language/locale code, and inject the <link rel="alternate" hreflang="..."> tags into the page head automatically.

When evaluating extensions, verify they:

  • Support self-referencing hreflang (each page includes a tag for itself)
  • Include x-default configuration
  • Handle both category and product page URL generation correctly
  • Work with your URL rewrite setup

Validate your Magento hreflang output

Check that your extension is generating correct hreflang tags across all store views.

Option 2: Custom Implementation via Layout XML

If you want full control without a third-party extension, you can implement hreflang via Magento's layout XML system.

Create a custom module with a layout update that adds hreflang tags to the <head> section. The core of this approach is a block class that:

  1. Gets all store views for the current website
  2. Maps each store view to its locale code
  3. Resolves the equivalent URL for the current page in each store view
  4. Outputs <link rel="alternate" hreflang="..."> tags

Here's the structure of a layout XML file that adds a hreflang block to all pages:

<!-- app/code/YourVendor/Hreflang/view/frontend/layout/default.xml -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <block class="YourVendor\Hreflang\Block\Hreflang"
               name="hreflang.tags"
               template="YourVendor_Hreflang::hreflang.phtml" />
    </head>
</page>

The block class fetches store views and their base URLs; the template renders the <link> tags. Getting the equivalent URL for the current page in each store view is the trickiest part -- you'll need to handle CMS pages, category pages, and product pages separately since their URL resolution differs.

URL rewrite handling

Magento's URL rewrite system means the same product can have different URL keys across store views (e.g., /en/blue-widget/ vs /fr/widget-bleu/). Your hreflang implementation must resolve the correct rewritten URL for each store view, not just swap the base URL.

Store View to Locale Mapping

Magento store views have a locale setting under Stores > Configuration > General > Locale Options. Your hreflang implementation should read this setting to determine the correct language code.

Common locale values and their hreflang equivalents:

Magento LocaleHreflang Value
en_USen-us
en_GBen-gb
fr_FRfr-fr
de_DEde-de
es_ESes-es
zh_Hans_CNzh-hans

Note that Magento uses underscores and can include script subtags; hreflang uses hyphens and typically only needs language + region (not script) for most use cases.

Testing Your Implementation

After implementation, verify hreflang is working correctly across all store views:

1

Check a product page in each store view

View source on a product page in each store view and confirm the full set of <link rel="alternate" hreflang="..."> tags is present, including self-referencing and x-default.

2

Verify URL accuracy

Each hreflang URL should be the correct localized URL for that product in that store view -- not just the base URL with a different prefix.

3

Check return links

Open each URL listed in the hreflang tags and confirm it also contains the full set of alternates. Every page in the cluster must link to every other page.

4

Validate with Google Search Console

After deploying, monitor the International Targeting report in GSC for errors. GSC will flag missing return links, incorrect locale codes, and other implementation issues.

Test with a crawler

Run a crawl with Screaming Frog or a similar tool set to extract hreflang data. This makes it easy to spot missing alternates, wrong locale codes, or redirect-broken URLs across your entire catalog.


Part of Boring Tools -- boring tools for boring jobs.

Generate perfect hreflang tags

Create and validate hreflang markup for your multilingual site. Free.