wintercms / wn-sitemap-plugin

Sitemap plugin for Winter CMS
https://wintercms.com/
MIT License
9 stars 5 forks source link

Add hreflang attribute to sitemap url entries #5

Closed der-On closed 1 year ago

der-On commented 2 years ago

For multilingual sites adding the hreflang attribute is required for correct SEO.

I would suggest that the locale property of a menu item should be used for that. If not present no hreflang attribute should be attached.

Problems to solve:

Automaticly add locale versions of static pages if the translate plugin is present and has more then one active locale.

I could provide a PR for that.

bennothommo commented 2 years ago

@der-On just curious - is there any benefit to the hreflang attribute being in the sitemap links as opposed to a meta tag on the pages themselves? The meta tag is something that can be done in the theme (or perhaps through the Translate plugin as a component).

der-On commented 2 years ago

@der-On just curious - is there any benefit to the hreflang attribute being in the sitemap links as opposed to a meta tag on the pages themselves? The meta tag is something that can be done in the theme (or perhaps through the Translate plugin as a component).

This would require including all locale variants of a page/url in the sitemap. The current implementation does not do this, but only lists the canonical urls of pages without a language prefix. Then the browser/session decides which language to display/redirect to. Not sure about how search engines do this. Maybe they index pages by mimicing various preferred languages?

I'm no SEO expert but just read that hreflang is a best practise for multilingual sitemaps.

Maybe I'm opening a can of worms here. I'll try to ask a SEO expert about this.

der-On commented 2 years ago

Another quick search returned this: https://developers.google.com/search/blog/2012/05/multilingual-and-multinational-site

So sitemaps should instead use alternative links:

<url>
  <loc>http://www.example.com/en</loc>
  <xhtml:link 
    rel="alternate"
    hreflang="de"
    href="http://www.example.com/de" />
  <xhtml:link
    rel="alternate"
    hreflang="en"
    href="http://www.example.com/en" />
</url>
<url>
  <loc>http://www.example.com/de</loc>
  <xhtml:link
    rel="alternate"
    hreflang="de"
    href="http://www.example.com/de" />
  <xhtml:link
    rel="alternate"
    hreflang="en"
    href="http://www.example.com/en" />
</url>
der-On commented 2 years ago

If including this in the core of the plugin is out of scope (which I can understand) adding an event to extend the generated sitemap would be good. That way one can add extra information as required for a use case or an additional plugin could do just that (like the translate or translate extended plugin).

mjauvin commented 2 years ago

@der-On please see this plugin: https://github.com/mjauvin/wn-mlsitemap-plugin

LukeTowers commented 2 years ago

Ideally the Winter.Translate plugin should be able to hook into this plugin to add the appropriate multilingual configuration automatically.

der-On commented 2 years ago

Ideally the Winter.Translate plugin should be able to hook into this plugin to add the appropriate multilingual configuration automatically.

This would require an event to be able to do so. Overriding the route completely (like done in the wn-mlsitemap-plugin) is not really a good option imho as it is not extendable. Any other plugin has to do the same and so no combinated functionality can be achieved.

LukeTowers commented 2 years ago

Yes, there has been a few conversations about adding an event in the past, I'm still open to it, just don't have time to do it myself.

mjauvin commented 2 years ago

Yes, I had to create this plugin in the past because I proposed adding an event and it didn't get accepted.

der-On commented 2 years ago

Yes, I had to create this plugin in the past because I proposed adding an event and it didn't get accepted.

Then let's do it again. @mjauvin do you want to redo the PR for the event or should I try an attempt?

mjauvin commented 2 years ago

Go for it @der-On !

LukeTowers commented 2 years ago

@mjauvin do you have a link to your original proposal?

mjauvin commented 2 years ago

@LukeTowers https://github.com/rainlab/sitemap-plugin/pull/74

LukeTowers commented 2 years ago

@der-On see https://github.com/rainlab/sitemap-plugin/compare/wip/extensibility for a starting point.

der-On commented 2 years ago

@LukeTowers Merged current main branch into your wip and made some minor adjustments https://github.com/wintercms/wn-sitemap-plugin/pull/6 Also tested it with my use case and it worked very well.

LukeTowers commented 2 years ago

@der-On are you able to also make a PR to the translate plugin so that it can hook into these events when present and automatically make the sitemap support the localized URLs available on the site?

der-On commented 2 years ago

@LukeTowers I can do.