Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Generating Sitemap file for aem sites

Avatar

Level 2

Hi Team, 
I am trying to create sitemap.xml file for my AEM sites using org.apache.sling.sitemap Api on AEM as Cloud Service,

Also i am able to genrate sitemap file, but the challenge, I am facing is 
I have multi brands sites on same domain,
Like i have www.CAP.edu and www.SAT.edu

The sitemap.xml file I am getting from my logic is:

1. For cap brand site

<url>
<loc>/content/cap</loc>
<lastmod>2024-08-29T10:57:56.314Z</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>/content/cap/en</loc>
<lastmod>2024-08-30T08:33:53.617Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<url>
 
2. For Sat brand site 
<url>
<loc>/content/sat</loc>
<lastmod>2024-08-30T08:55:14.693Z</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>/content/sat/linktemptest</loc>
<lastmod>2023-09-22T12:10:02.475Z</lastmod>
<changefreq>yearly</changefreq>
<priority>0.8</priority>
</url>
<url>

 

But i want there should be domain name in each sitemap file.

Expected Result :

1. For cap site

<url>
<lastmod>2024-05-10</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
<url>
<lastmod>2024-04-30</lastmod>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
</url>
 
2. For sat brand site 
<url>
<lastmod>2024-05-09</lastmod>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
</url>
<url>
<lastmod>2024-05-09</lastmod>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
</url>
<url>
2 Replies

Avatar

Community Advisor

Hi @AkshaySwamiii ,

You can try to use externalizer in your custom code to get the URLs with domain. Also, if you have multi territory site then you can include country code in it.
Sample code would look something like this:

@Reference
private Externalizer externalizer; 

final String location = this.externalizer.publishLink(
                userService.getServiceResolver("<sample-resolver>"),
                resource.getPath());             


Use com.day.cq.commons.impl.ExternalizerImpl.cfg.json system console configuration define as part of config.publish for having externalizer domain as part of every URL as shown below coming as part of Sitemap. It will be different depend on the run mode or environment. 

 

{
 "externalizer.domains": [
  "publish https://sampledomain.com"
 ]
}

 

-Tarun

Avatar

Level 2

if i do these changes in my servlet, then servlet is not registering.
My current code is

@Override
    protected void addResource(final String name, final Sitemap sitemap, final Resource resource)
            throws SitemapException {

        final Page page = resource.adaptTo(Page.class);
        if (page == null) {
        	COMMON_LOGGER.logError(CLASS_NAME, resource.getPath());
            return;
        }
        COMMON_LOGGER.logInfo(CLASS_NAME,resource.getPath());
        final String location = this.externalizer.externalize(resource);
        COMMON_LOGGER.logInfo(CLASS_NAME, location);

        // Add URL to sitemap
        final Url url = sitemap.addUrl(location);
        final Calendar lastmod = Optional.ofNullable(page.getLastModified())
                                         .orElse(page.getContentResource()
                                                     .getValueMap()
                                                     .get(JcrConstants.JCR_CREATED, Calendar.class));
        if (lastmod != null) {
            url.setLastModified(lastmod.toInstant());
        }

        // Dynamically determine changefreq based on logic
        final Url.ChangeFrequency changeFrequency = determineChangeFrequency(lastmod);
        url.setChangeFrequency(changeFrequency);

        // Dynamically determine priority based on logic
        final double priority = determinePriority(page);
        url.setPriority(priority);

        COMMON_LOGGER.logDebug(CLASS_NAME, url.toString());
    }

.