Ad

Relate HREFLANG Tags To The Current Page

- 1 answer

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" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" 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" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="https://www.example.com/products" hreflang="en" /> and/or <link rel="alternate" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="https://www.example.com/products/tabletimes" hreflang="en" />?

Ad

Answer

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" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" 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.

Ad
source: stackoverflow.com
Ad