Generating Sitemap file for aem sites | Community
Skip to main content
Level 2
September 9, 2024

Generating Sitemap file for aem sites

  • September 9, 2024
  • 1 reply
  • 833 views

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>
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

TarunKumar
Community Advisor
Community Advisor
September 9, 2024

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

Level 2
September 9, 2024

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

@9944223 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()); }

.