Search code examples
htmlseohrefhead

Relate HREFLANG tags to the current page


I've been tasked with updating our hreflang tags to relate to the site that the user is on. For example...

Currently it's hard coded like <link rel="alternate" href="https://www.example.com/" hreflang="en" /> but say the user has navigated to the products/tabletimes page so the URL is actually https://www.example.com/products/tabletimes. Is there a way to code the tags in <head> to reflect this?

So instead it could be <link rel="alternate" href="https://www.example.com/products" hreflang="en" /> and/or <link rel="alternate" href="https://www.example.com/products/tabletimes" hreflang="en" />?


Solution

  • After looking thoroughly through the Context variable I found that the Request.Url.AbsolutePath property does relate to the path of the page you're on. You can use that property in <link rel="alternate" href="@string.Format("https://www.example.com{0}", absoluteUrlPath)" hreflang="en" /> to reflect which page you're on in the other corresponding laguages that have a matching page.