We're looking to be able to manually edit the hreflang tags in the <head> of our page, to improve our SEO potential. We have a fairly simple custom solution (rather than using Hubspot's built in language tools), that is as follows:
{% if request.path == "/us/" %}
[ Two link tags to the homepage, with the US page as primary and UK page as alternate ]
{% elif request.path is string_containing "/us/" %}
[ Two link tags, with the US page as primary and UK page as alternate ]
{% endif %}
{% if request.path == "/uk/" %}
[ Two link tags to the homepage, with the UK page as primary and US page as alternate ]
{% elif request.path is string_containing "/uk/" %}
[ Two link tags, with the UK page as primary and US page as alternate ]
{% endif %}
The issue arises when:
- We have a page only available in one language, or
- We have a page where there's a different spelling of a word in the URL from US to UK. 'programme' or 'optimisation' for example.
Solutions tried so far:
- Moving this code into a module and adding variables that would let us manually set the links. This did not work however, as the module is moved into the <body> tag by Hubspot automatically.
- Adding the specific page's links to its header under the 'Settings' tab for the page. Not suitable however, as the hreflang links need to be the first thing inside the <head> tag.
Possible solutions:
- Use Javascript to insert the hreflang links in <head> post-page render. However this may be unreliable for 100% of page loads, and/or when the pages are crawled by Google.
- Clone the page template for each page - not really viable time-wise, or version control.
- Manually hard-code each exception to the rule into the original if/elif statements - may be tricky but would give the right outcome.
- Other - has anyone else come up against this problem and has a solution?
Hello @JBlay, thank you for posting in our Community! Thank you for providing details, it is recommended to implement it with a designer or someone who is familiar with HTML and CSS.
Thanks for thinking of me @PamCotton. This coding is a bit above my head, but I'm going to share it with my colleagues to see if we have ideas for @JBlay🙂
Did my answer help? Please "mark as a solution" to help others find answers. Plus I really appreciate it!