Many websites serve users all around the world with content translated or targeted to users in a certain region. Google uses the
rel="alternate" hreflang="x" attributes to serve the correct language or regional URL in the search results. The hreflang attribute enables a search engine to understand the language and geographical set up of a website.
There are 3 scenarios that can be covered with the hreflang attribute:
- Same country, different languages.
- Different countries, different languages.
- Different countries, same language.
If the hreflang attribute is not applied or is applied incorrectly it will mean your target audience will be landing on a page not targeted for them. If you want to improve your visibility in the search engine listing, volume of traffic and bounce rate then use the hreflang attribute carefully.
How to use the hreflang attribute?
The hreflang attribute can be implemented in three ways:
- HTML head – using header link elements, a developer can add code to the
<head>section of every page.
For dynamic page templates in WordPress, see image , example
- HTTP header – the solution for all your PDFs and other non-HTML content you might want to optimize. Link elements work nicely for HTML documents, but not for other types of content as you can’t include them. That’s where HTTP headers come in. They should look like this.
- XML sitemaps – using XML sitemap markup. It uses the xhtml:link attribute in XML sitemaps to add the annotation to every URL. It works very much in the same way as you would in a page’s <head> with <link> elements. If you thought link elements were verbose, the XML sitemap implementation is even worse. This is the markup needed for just one URL with two other languages.
Supported languages and region codes
The value of the hreflang attribute defines the language of alternate URLs. For language codes use the ‘ISO 639-1‘ standard, for country codes use the ‘ISO 3166-1‘ standard.
If your site only targets different languages, you only need ISO 639-1. If you need distinction between region and language, then you also need ISO 3166-1.
- en: English content, not restricted to on any specific region
- en-US: English content, targeted at the US market
- en-MX: English content, targeted at the Mexican market