Expand my Community achievements bar.

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

Avatar

Level 2

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?"

2 Replies

Avatar

Community Advisor

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