Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect canonical and alternative URL annotations #2159

Merged
merged 7 commits into from
Apr 9, 2024

Conversation

KentarouTakeda
Copy link
Contributor

Check List

  • Others (Update, fix, translation, etc...)

Summary

There is a problem with the canonical and alternative URL annotations in the <head> tag of the page where the translation exists.

This is probably why when we search for "hexo" on Google, we get strange results in some languages.

search-result-of-google

This pull request provides corrections to errors and improvements in <head>, with the aim of displaying search results as naturally as possible.

Current <head> tag

For example, https://hexo.io/zh-tw/api/ outputs the following (excerpt):

<html lang="zh-tw">

<head prefix="og: http://ogp.me/ns#">
  <link rel="canonical" href="https://hexo.io/zh-tw/api/index.html"> 

  <link rel="alternative" hreflang="en" href="https://hexo.io/zh-tw/api/">
  <link rel="alternative" hreflang="zh-tw" href="https://hexo.io/zh-tw/zh-tw/api/">
  <link rel="alternative" hreflang="zh-cn" href="https://hexo.io/zh-cn/zh-tw/api/">
  <!-- ... -->

For this example:

  • The canonical tag does not properly indicate a canonical URL because the pretty_urls.trailing_index: false setting means the URL should end with '/'.
  • The term alternative is incorrect. The correct term is alternate.
  • The alternate tag incorrectly points to URLs with duplicate :lang slugs, leading to either nonexistent URLs or URLs that incorrectly display content in English despite indicating a different language.
  • alternative again points to the URL for the language being displayed (zh-tw in this example).

There is a problem as above. In the screenshot at the beginning, it seems that Google is unable to recognize the correspondence between pages in each language, and this is probably the cause.

Fixes and improvements with this pull request

  • Fixed the problem with the canonical tag mentioned above. Omit index.html at the end.
  • Corrected alternative to alternate above.
  • Corrected the error in the URL pointed to by alternate above.
  • As mentioned above, alternate does not output the displayed URL itself.
  • alternative is output in /:lang/about/ but the page does not exist. Delete this.
  • Annotation with English as default for User Agents in languages for which no translation exists.

As a result of these modifications, the URL annotation will look like this (excerpt):

<html lang="zh-tw">

<head prefix="og: http://ogp.me/ns#">
  <link rel="canonical" href="https://hexo.io/zh-tw/api/">

  <link rel="alternate" hreflang="en" href="https://hexo.io/api/">
  <link rel="alternate" hreflang="zh-cn" href="https://hexo.io/zh-cn/api/">
  <link rel="alternate" hreflang="x-default" href="https://hexo.io/api/" />
  <!-- ... -->
<html lang="en">

<head prefix="og: http://ogp.me/ns#">
  <link rel="canonical" href="https://hexo.io/about/">

  <!-- no `alternate` is output -->
  <!-- ... -->

Reference

Add <link rel="alternate" hreflang="lang_code"... > elements to your page header to tell Google all of the language and region variants of a page. This is useful if you don't have a sitemap or the ability to specify HTTP response headers for your site.

Consider adding a fallback page for unmatched languages, especially on language/country selectors or auto-redirecting home pages. Use the the x-default value:
<link rel="alternate" href="https://example.com/" hreflang="x-default" />

@github-actions github-actions bot added the infra infrastructure, configuration of this repo label Apr 6, 2024
Copy link
Contributor

github-actions bot commented Apr 6, 2024

Theme name validation passed.
Theme thumbnails validation completed.

@uiolee
Copy link
Member

uiolee commented Apr 7, 2024

Looks like it doesn't work properly in /news/
news

@KentarouTakeda
Copy link
Contributor Author

Thanks! Fixed in 7c9847e.

For pages where permalink is not set, the same URL as hreflang="x-default" is used for canonical.
Although the implementation has changed, the result remains the same (other than removing index.html).

@SukkaW SukkaW merged commit d2192c7 into hexojs:master Apr 9, 2024
6 checks passed
@KentarouTakeda KentarouTakeda deleted the seo-improvement branch April 9, 2024 10:19
@KentarouTakeda
Copy link
Contributor Author

Thanks for the review and merge!

I believe that by improving search results, more people will use this amazing tool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infra infrastructure, configuration of this repo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants