📖 Scenario: You manage a website that serves users in different countries and languages. To help search engines show the right version of your pages to the right users, you need to use hreflang tags correctly.This project will guide you step-by-step to add hreflang tags to your HTML pages for international SEO.
🎯 Goal: Build a set of hreflang tags in the HTML <head> section that correctly link language and regional versions of a webpage.
📋 What You'll Learn
Create a list of URLs for different language versions of a page
Define the language and region codes for each URL
Write hreflang link tags for each language version
Add a default hreflang tag for users without a specific language preference
💡 Why This Matters
🌍 Real World
Websites that serve users in multiple languages and countries use hreflang tags to tell search engines which page version to show to which user.
💼 Career
SEO specialists and web developers use hreflang tags to improve international search rankings and user experience.
Progress0 / 4 steps
1
Create a list of page URLs for different languages
Create a list called page_urls containing these exact URLs as strings: "https://example.com/en-us", "https://example.com/fr-fr", "https://example.com/es-es".
SEO Fundamentals
Hint
Use square brackets [] to create a list and include the URLs as strings inside quotes.
2
Define language-region codes for each URL
Create a list called hreflang_codes with these exact strings: "en-US", "fr-FR", "es-ES".
SEO Fundamentals
Hint
Use a list of strings matching the language and region codes for each URL.
3
Write hreflang link tags for each language version
Use a for loop with variables url and code to iterate over zip(page_urls, hreflang_codes). Inside the loop, create a string called link_tag with this exact format: <link rel="alternate" hreflang="{code}" href="{url}" /> using f-strings.
SEO Fundamentals
Hint
Use zip() to pair URLs and codes, then create the link tag string with f-strings.
4
Add a default hreflang tag for unspecified languages
Add a string variable called default_tag with this exact value: <link rel="alternate" hreflang="x-default" href="https://example.com/en-us" />.
SEO Fundamentals
Hint
The x-default hreflang tag is used as a fallback for users without a matching language.
Practice
(1/5)
1. What is the main purpose of using hreflang tags in international SEO?
easy
A. To tell search engines which language or regional version of a page to show users
B. To increase the number of backlinks to a website
C. To speed up website loading times
D. To hide duplicate content from search engines
Solution
Step 1: Understand the role of hreflang tags
hreflang tags tell search engines which language or regional version of a page should be shown to users based on their location or language preference.
Step 2: Compare with other SEO goals
While backlinks, speed, and duplicate content are important SEO factors, hreflang specifically addresses language and regional targeting.
Final Answer:
To tell search engines which language or regional version of a page to show users -> Option A
Quick Check:
Purpose of hreflang = language/region targeting [OK]
Hint: Remember: hreflang = language + region targeting [OK]
Common Mistakes:
Confusing hreflang with backlink strategies
Thinking hreflang speeds up the site
Assuming hreflang hides duplicate content
2. Which of the following is the correct syntax to specify a hreflang tag for a Spanish page targeting Spain?
easy
A.
B.
C.
D.
Solution
Step 1: Identify correct hreflang format
The correct format uses a hyphen between language and region codes, like "es-ES" for Spanish in Spain.
Step 2: Check href URL matches language and region
The URL should point to the Spanish page, so "https://example.com/es/" is correct. points to English, so it's incorrect.
Final Answer:
<link rel="alternate" hreflang="es-ES" href="https://example.com/es/" /> -> Option C
Quick Check:
Correct hreflang format = language-region with hyphen [OK]
Hint: Use hyphen between language and region codes [OK]
Common Mistakes:
Using underscore instead of hyphen
Mismatch between hreflang and URL language
Omitting region code when needed
3. Given the following hreflang tags in the <head> section:
5. You manage a website with English, French, and Spanish versions. You want to target users in Canada who speak English and French, and users in Spain who speak Spanish. Which hreflang tags should you use to correctly target these audiences?
hard
A.
B.
C.
D.
Solution
Step 1: Match language and region codes to target audiences
Canada English is "en-CA", Canada French is "fr-CA", and Spain Spanish is "es-ES". These codes precisely target the intended users.
Step 2: Include x-default for fallback
The x-default tag provides a default page for users not matching any specific hreflang, improving user experience and SEO.
Final Answer:
Use hreflang tags with en-CA, fr-CA, es-ES, and x-default for fallback -> Option D
Quick Check:
Use language-region codes + x-default for precise targeting [OK]
Hint: Use language-region codes plus x-default for fallback [OK]
Common Mistakes:
Using generic language codes without region
Mixing up region codes (e.g., US instead of CA)
Omitting x-default tag
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.