Jump to content

Villiam

Member
  • Posts

    2
  • Joined

  • Last visited

Everything posted by Villiam

  1. If you don't have a logic URL structure, you might want to use this code instead and adjust it manually for each page depending on the URL: <script> function addHreflangTags() { var linkSV = document.createElement('link'); linkSV.rel = "alternate"; linkSV.href = "https://www.examplesite.com/sv/blog/sem-vs-seo"; linkSV.hreflang = "sv"; var linkEN = document.createElement('link'); linkEN.rel = "alternate"; linkEN.href = "https://www.examplesite.com/en/blog/sem-vs-seo"; linkEN.hreflang = "en"; var linkXDefault = document.createElement('link'); linkXDefault.rel = "alternate"; linkXDefault.href = "https://www.examplesite.com/en/blog/sem-vs-seo"; // English as x-default linkXDefault.hreflang = "x-default"; document.head.appendChild(linkSV); document.head.appendChild(linkEN); document.head.appendChild(linkXDefault); } window.onload = addHreflangTags; </script>
  2. I think I've found a decent solution. As @lyssipos explains, hreflang-tags in the body aren't of value to Google. I used Gemini to write me some javascript that lets me add hreflang-tags to the header using codeblocks. This is my code but bear in mind you're going to have to adjust it according to your website's structure, this is based on my site that uses subdirectories for /en and /sv: function addHreflangTags() { const currentURL = window.location.href; // *** ADJUST: Extract the language code from your URL structure *** const languageCode = currentURL.split('/')[3]; // Example, change the index if needed // *** ADJUST: Your website's base URL *** const baseURL = "https://www.example.com/"; const alternateLang = (languageCode === 'en') ? 'sv' : 'en'; const alternateURL = baseURL + alternateLang + currentURL.substring(baseURL.length + languageCode.length); // Create links const linkCurrent = document.createElement('link'); linkCurrent.rel = "alternate"; linkCurrent.href = currentURL; linkCurrent.hreflang = languageCode; const linkAlternate = document.createElement('link'); linkAlternate.rel = "alternate"; linkAlternate.href = alternateURL; linkAlternate.hreflang = alternateLang; const linkXDefault = document.createElement('link'); linkXDefault.rel = "alternate"; linkXDefault.href = baseURL + 'en' + currentURL.substring(baseURL.length + languageCode.length); linkXDefault.hreflang = "x-default"; // Append links to the head document.head.appendChild(linkCurrent); document.head.appendChild(linkAlternate); document.head.appendChild(linkXDefault); } window.onload = addHreflangTags;
×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.