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.
Create a Custom Servlet Resolver: Implement a custom servlet resolver that modifies the generated sitemap URLs.
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