How to overwrite the hostname while generating a sitemap XML using an on-demand approach. | Community
Skip to main content
Ayoub_Tabai
New Participant
February 7, 2024
Solved

How to overwrite the hostname while generating a sitemap XML using an on-demand approach.

  • February 7, 2024
  • 2 replies
  • 880 views

I've created a sitemap XML for our site using Apache Sling Sitemap - Sitemap Generator Manager. However, I'm looking for a way to change the hostname in this section: <loc>http://localhost:4502/content/blablabla/us.sitemap.test7-sitemap.xml</loc>. I want to remove the /content part. Any suggestions?"

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by partyush

Hi @ayoub_tabai 

To overwrite the hostname while generating a sitemap XML using Apache Sling Sitemap - Sitemap Generator Manager and remove the /content part from the <loc> section, you can achieve this by configuring the Sitemap Manager to use a custom servlet resolver.

  1. Create a Custom Servlet Resolver: Implement a custom servlet resolver that modifies the generated sitemap URLs.

  2. Register the Custom Servlet Resolver: Register the custom servlet resolver with the appropriate configurations.

Here's how you can do it:

 

import org.apache.sling.sitemap.SitemapGeneratorManager; import org.apache.sling.sitemap.SitemapResolver; import org.apache.sling.sitemap.spi.generator.SitemapGenerator; import org.apache.sling.sitemap.spi.generator.SitemapGeneratorFactory; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @Component(service = SitemapResolver.class) public class CustomSitemapResolver implements SitemapResolver { @Reference private SitemapGeneratorFactory sitemapGeneratorFactory; @Override public SitemapGenerator resolve(String sitemapName) { SitemapGenerator generator = sitemapGeneratorFactory.createGenerator(sitemapName); if (generator != null && generator.getGeneratorId().equals("sling:basic")) { // Customize the generator's URLs here generator.setHostname("your-custom-hostname.com"); } return generator; } }

 

In this custom resolver:

  • We implement the SitemapResolver interface to customize the behavior of sitemap generation.
  • We reference the SitemapGeneratorFactory to create generators for different sitemaps.
  • We override the resolve method to modify the generated sitemap URLs. Here, we check if the generator ID is sling:basic (the default generator provided by Sitemap Generator Manager) and customize the hostname.

Make sure to replace "your-custom-hostname.com" with your actual hostname.

Once you've created and registered the custom resolver, it will intercept sitemap generation requests and modify the URLs accordingly, removing the /content part and replacing the hostname.

This approach ensures that the modification is applied dynamically during sitemap generation, without the need to manipulate the generated XML afterward.

 

 

Thanks 

2 replies

Imran__Khan
New Participant
February 11, 2024
partyush
partyushAccepted solution
New Participant
February 8, 2024

Hi @ayoub_tabai 

To overwrite the hostname while generating a sitemap XML using Apache Sling Sitemap - Sitemap Generator Manager and remove the /content part from the <loc> section, you can achieve this by configuring the Sitemap Manager to use a custom servlet resolver.

  1. Create a Custom Servlet Resolver: Implement a custom servlet resolver that modifies the generated sitemap URLs.

  2. Register the Custom Servlet Resolver: Register the custom servlet resolver with the appropriate configurations.

Here's how you can do it:

 

import org.apache.sling.sitemap.SitemapGeneratorManager; import org.apache.sling.sitemap.SitemapResolver; import org.apache.sling.sitemap.spi.generator.SitemapGenerator; import org.apache.sling.sitemap.spi.generator.SitemapGeneratorFactory; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @Component(service = SitemapResolver.class) public class CustomSitemapResolver implements SitemapResolver { @Reference private SitemapGeneratorFactory sitemapGeneratorFactory; @Override public SitemapGenerator resolve(String sitemapName) { SitemapGenerator generator = sitemapGeneratorFactory.createGenerator(sitemapName); if (generator != null && generator.getGeneratorId().equals("sling:basic")) { // Customize the generator's URLs here generator.setHostname("your-custom-hostname.com"); } return generator; } }

 

In this custom resolver:

  • We implement the SitemapResolver interface to customize the behavior of sitemap generation.
  • We reference the SitemapGeneratorFactory to create generators for different sitemaps.
  • We override the resolve method to modify the generated sitemap URLs. Here, we check if the generator ID is sling:basic (the default generator provided by Sitemap Generator Manager) and customize the hostname.

Make sure to replace "your-custom-hostname.com" with your actual hostname.

Once you've created and registered the custom resolver, it will intercept sitemap generation requests and modify the URLs accordingly, removing the /content part and replacing the hostname.

This approach ensures that the modification is applied dynamically during sitemap generation, without the need to manipulate the generated XML afterward.

 

 

Thanks