AEM Sitemap Externalize and Extension less URL
This blog will help us to create a Sitemap for AEM as a cloud service AEMaaCS having externalize URL and remove html from the same sitemap URL’s as shown below.
Follow below blogs to read more about Externalizer and setup a Sitemap.
Externalizer Configuration
Use com.day.cq.commons.impl.ExternalizerImpl.cfg.json system console configuration define as part of config.publish for having externalizer domain like https://javadoubts.com 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://javadoubts.com"
]
}
Link checker and transformer Configuration
We are going to use LinkcheckerTransformer configuration (com.day.cq.rewriter.linkchecker.impl.LinkCheckerTransformerFactory.cfg.json) to remove html from URL coming as part of sitemap
{
"linkcheckertransformer.strictExtensionCheck": false,
"linkcheckertransformer.rewriteElements": [
"a:href",
"area:href",
"form:action"
],
"linkcheckertransformer.disableRewriting": false,
"linkcheckertransformer.disableChecking": true,
"linkcheckertransformer.stripHtmltExtension": true,
"linkcheckertransformer.mapCacheSize": 5000
}
linkcheckertransformer.stripHtmltExtension as true, will allow us to remove or strip html form the URL.
Follow URL to read more about Link checker and transformer.
Custom Sitemap Implementation
Create custom class to implement Sitemap having externalize URL and remove or strip .html from the URL.
package com.javadoubts.core.generator;
import com.day.cq.commons.Externalizer;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.Page;
import com.javadoubts.core.services.UserService;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.sitemap.SitemapException;
import org.apache.sling.sitemap.builder.Sitemap;
import org.apache.sling.sitemap.builder.Url;
import org.apache.sling.sitemap.spi.generator.ResourceTreeSitemapGenerator;
import org.apache.sling.sitemap.spi.generator.SitemapGenerator;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.propertytypes.ServiceRanking;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Calendar;
import java.util.Optional;
@Component(service = SitemapGenerator.class)
@ServiceRanking(20)
public class JavadoubtsSitemapGenerator extends ResourceTreeSitemapGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(JavadoubtsSitemapGenerator.class);
@Reference
UserService userService;
/*
* Externalizer domian will get pick as part of Externalizer
* configuration defined as part of earlier step.
*/
@Reference
private Externalizer externalizer;
@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)
return;
/*
* below code will generate url having domian appended.
*/
final String location = this.externalizer.publishLink(
userService.getServiceResolver("javadoubtsSystemUser"),
resource.getPath());
// added domian appended URL within sitemap.
final Url pageUrl = sitemap.addUrl(location);
final Calendar lastmodifiedDate = Optional.ofNullable(page.getLastModified())
.orElse(page.getContentResource()
.getValueMap()
.get(JcrConstants.JCR_CREATED, Calendar.class));
if (lastmodifiedDate != null) {
pageUrl.setLastModified(lastmodifiedDate.toInstant());
}
}
}
Go to browser and check for sitemap https://javadoubts.com/sitemap.xml
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.