HREFLANG IMPLEMENTATION
UNSING XML SITEMAP

Quickly and easily generate an XML Sitemap with alternate tags

Sajid Hasan Sifat
5 min readApr 14, 2020

If you have multiple versions of a page for different languages or regions, tell Google about these different variations. Doing so will help Google Search point users to the most appropriate version of your page by language or region.

Sitemap Schema:

To generate a sitemap first we need to know about the sitemap schema.

The Sitemap must:

Begin with an opening <urlset> tag and end with a closing </urlset> tag.

Specify the namespace (protocol standard) within the <urlset> tag.

Include a <url> entry for each URL, as a parent XML tag.

Include a <loc> child entry for each <url> parent tag.

Also, all URLs in a Sitemap must be from a single host, such as www.lemonhive.com or en.lemonhive.com.

Sample XML Sitemap

The following example shows a Sitemap that contains just one URL and uses all optional tags.

<?xml version=”1.0" encoding=”UTF-8"?>
<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005–01–01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>

HREFLANG TAG:

According to google,

If you have multiple versions of a page for different languages or regions, tell Google about these different variations. Doing so will help Google Search point users to the most appropriate version of your page by language or region.

Note that even without taking action, Google might still find alternate language versions of your page, but it is usually best for you to explicitly indicate your language- or region-specific pages.

Some example scenarios where indicating alternate pages is recommended:

If you keep the main content in a single language and translate only the template, such as the navigation and footer. Pages that feature user-generated content, like forums, typically do this.

If your content has small regional variations with similar content, in a single language. For example, you might have English-language content targeted to the US, GB, and Ireland.

If your site content is fully translated into multiple languages. For example, you have both German and English versions of each page.

SITEMAP RULES FOR HREFLANG:

Specify the xhtml namespace as follows:
xmlns:xhtml=”http://www.w3.org/1999/xhtml"

Create a separate <url> element for each URL.

Each <url> element must include a <loc> child indicating the page URL.

Each <url> element must have a child element <xhtml:link rel=”alternate” hreflang=”supported_language-code”> that lists every alternate version of the page, including itself. The order of these child <xhtml:link> elements doesn’t matter, though you might want to keep them in the same order to make them easier for you to check for mistakes.

Example

Here is an English language page targeted at English speakers worldwide, with equivalent versions of this page targeted at German speakers worldwide and German speakers located in Switzerland. Here are all the URLs present on your site:

www.example.com/english/page.html, targeted at English speakers.

www.example.com/deutsch/page.html targeted at German speakers.

www.example.com/schweiz-deutsch/page.html targeted at German speakers in Switzerland.

Here is the sitemap for those three pages:

<?xml version=”1.0" encoding=”UTF-8"?>
<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml=”http://www.w3.org/1999/xhtml">
<url>
<loc>http://www.example.com/english/page.html</loc>
<xhtml:link rel=”alternate” hreflang=”de” href=”http://www.example.com/deutsch/page.html”/>
<xhtml:link rel=”alternate” hreflang=”de-ch” href=”http://www.example.com/schweiz-deutsch/page.html"/>
<xhtml:link
rel=”alternate” hreflang=”en” href=”http://www.example.com/english/page.html”/>
</url>
<url>
<loc>http://www.example.com/deutsch/page.html</loc>
<xhtml:link rel=”alternate” hreflang=”de” href=”http://www.example.com/deutsch/page.html”/>
<xhtml:link
rel=”alternate” hreflang=”de-ch” href=”http://www.example.com/schweiz-deutsch/page.html"/>
<xhtml:link rel=”alternate” hreflang=”en” href=”http://www.example.com/english/page.html”/>
</url>
<url>
<loc>http://www.example.com/schweiz-deutsch/page.html</loc>
<xhtml:link rel=”alternate” hreflang=”de” href=”http://www.example.com/deutsch/page.html”/>
<xhtml:link rel=”alternate” hreflang=”de-ch” href=”http://www.example.com/schweiz-deutsch/page.html"/>
<xhtml:link rel=”alternate” hreflang=”en” href=”http://www.example.com/english/page.html”/>
</url>
</urlset>

IMPORTANT FEATURES:

  • Self-reference hreflang.
  • Page should not reference for more than one language.
  • Missing reciprocal hreflang.
  • Hreflang to non-canonical. ***
  • Hreflang and HTML lang mismatch.
  • X‑default hreflang annotation missing.

SELF-REFERENCE HREFLANG:

This issue reports pages declared as alternate language versions that don’t list themselves in their hreflang annotations.

Example:

Hreflang annotations for https://lemonhive.com/us/blog :

<link rel=”alternate” hreflang=”x-default” href=”https://lemonhive.com/blog/" />
<link rel=”alternate” hreflang=”en-gb” href=”https://lemonhive.com/uk/blog/" />
<link rel=”alternate” hreflang=”ru” href=”https://lemonhive.com/ru/blog/" />

The page must reference itself in its hreflang annotations. The following line is missing:

<link rel=”alternate” hreflang=”en-us” href=”https://lemonhive.com/us/blog/" />

FOR SITEMAP:

<url><loc>https://lemonhive.com/us/blog/</loc>
<xhtml:link rel=”alternate” hreflang=”x-default” href=”https://lemonhive.com/blog “ />
<xhtml:link rel=”alternate” hreflang=”en-gb” href=”https://lemonhive.com/uk/blog" />
<xhtml:link rel=”alternate” hreflang=”en-us” href=”https://lemonhive.com/us/blog" />
</url>

Hreflang to non-canonical:

rel=”alternate” hreflang=”x”markup and rel=”canonical” can and should be used together. Every language should have a rel=”canonical” link pointing to itself. In the first example, this would look like this, assuming that we’re on the example.com homepage:

<url><loc>https://lemonhive.com/</loc>
< xhtml:link rel=”canonical” href=”http://lemonhive.com/">
< xhtml:link rel=”alternate” href=”http://lemonhive.com/" hreflang=”en” />
< xhtml:link rel=”alternate” href=”http:// lemonhive.com/en-gb/” hreflang=”en-gb” />
< xhtml:link rel=”alternate” href=”http:// lemonhive.com/en-au/” hreflang=”en-au” />
</url>

If we were on the en-gb page, only the canonical would change:

<link rel=”canonical” href=”http:// lemonhive.com/en-gb/”>
<link rel=”alternate” href=”http:// lemonhive.com/” hreflang=”en” /><link rel=”alternate” href=”http:// lemonhive.com/en-gb/” hreflang=”en-gb” />
<link rel=”alternate” href=”http:// lemonhive.com/en-au/” hreflang=”en-au” />

Here is a Google spreadsheet where you can easily create your href lang sitemap xmp file.

image by author

First copy the sreadsheet into your own Drive.
If you open you will have something like in the picture in the left.
Put you Urls and your respective international Urls in the second column.
[you can add more column if you can understand the simple formula]

image by author

Chose your country and language.
After filling all the urls , country and language fields, the XML column will be filled automatically.

image by author

After that create an empty sitemap_hreflanf.xml in your directory.
Write this code in the file :

<?xml version=”1.0" encoding=”UTF-8"?>
<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml=”http://www.w3.org/1999/xhtml">
///paste your code here///
</urlset>
image by author

Copy the XML CODE column and paste it in the sitemap_hreflanf.xml file under the line
“///paste your code here///”

save the file.

Now you have you hreflang sitemap xml file which you can put in your Google Search Console.

Conclusion:

Thank you for reading.
You can also have more knowledge from :

[1] MOZ blog, https://moz.com/learn/seo/hreflang-tag

[2] Google Support, https://support.google.com/webmasters/answer/189077?hl=en.

--

--