getgrav / grav-plugin-sitemap

Grav Sitemap Plugin
https://getgrav.org
MIT License
42 stars 42 forks source link

Sitemap missing url elements of page translations #59

Closed vangogs closed 3 years ago

vangogs commented 6 years ago

According to sitemap specification https://support.google.com/webmasters/answer/189077?hl=en If page X links to page Y, page Y must link back to page X. If this is not the case for all pages that use hreflang annotations, those annotations may be ignored or not interpreted correctly.

It means that sitemap must generate URL elements for all pages, including all translations. As it is in example (on the same page).

But currently it generates only all pages once with translations added as alternate links.

Attaching file with fix which works on multilingual site sitemap.php.zip

Basically I have added another loop to create url enties for all translations too

See:

            $entry_translated = $page->translatedLanguages();
            foreach($entry_translated as $trans_lang => $trans_page_route) {

                $entry = new SitemapEntry();
                //$entry->location = $page->canonical();
                $entry_route = $this->grav['language']->getLanguageURLPrefix($trans_lang).'/'.$trans_page_route;
                $entry->location = $rootUrl.$entry_route;

And changed url generation for alternate links, because instead of page url it was using the route which may be different for pages if slug is entered (which is not the same as folder)

                    foreach($entry_translated as $lang => $page_route) {
                        //$page_route = $page->rawRoute();
                        if ($page->home()) {
                            $page_route = '';
                        }

                        $entry->translated[$lang] = '/'.$page_route;
nbusseneau commented 3 years ago

Will be fixed by 856aea099cf5b62ba566669c9a2bf9f66d6f35ea in 3.0.0, see #83 for history.

rhukster commented 3 years ago

Multilang support has been rewritten and significantly improved. Please try v3.0