Hello!
Homepage in sitemap.xml generated by this module always has a trailing slash. Is it possible to remove it (or make optional)?

Comments

Terris created an issue. See original summary.

er.garg.karan’s picture

Version: 7.x-2.2 » 8.x-1.0

I am looking for the same in Drupal 8 as well as Drupal 9 version of this module.

3CWebDev’s picture

Placing something like this in a custom module will remove the trailing slash:

function MY_MODULE_xmlsitemap_element_alter(array &$element, array $link, \Drupal\xmlsitemap\XmlSitemapInterface $sitemap) {
  if ($element['loc'] == 'https://DOMAIN_NAME.com/'){
    $element['loc'] = 'https://DOMAIN_NAME';
  }
}
Dave Reid’s picture

Category: Bug report » Feature request
Status: Active » Postponed (maintainer needs more info)

It's worth asking, why does it matter? Until there's a good reason why I'm going to recategorize this as a feature request, not a bug.

3CWebDev’s picture

The SEO team for our client requested the change. They said there is a slight issue because the domain shouldn't have a trailing slash. I doubt it matters, honestly, but they made the request and I saw this thread where someone else had requested it.

I agree, thought, it's not a bug. Perhaps an option to remove it through config as a feature request? But, with the hook it's easy enough to do without any module change.

Dave Reid’s picture

Unless there's a documented need as to why this feature would be necessary, I'm just not sure it's worth the time supporting this officially. Until we can solve that first part, I would be inclined to just mark this as resolved since we have a documented way to accomplish this with the hook, it could be even extended to trim any trailing slashes from paths:

function MY_MODULE_xmlsitemap_element_alter(array &$element, array $link, \Drupal\xmlsitemap\XmlSitemapInterface $sitemap) {
  $element['loc'] = rtrim($element['loc'], '/');
}