Sitemap custom cannot be accessed on my AEM website | Community
Skip to main content
New Participant
December 18, 2024
Solved

Sitemap custom cannot be accessed on my AEM website

  • December 18, 2024
  • 4 replies
  • 1455 views

Hello, what happens is that I am making a custom sitemap, this has as name “products-sitemap.xml”

This sitemap is created by searching all the content fragments of a folder called cf-products.

I already tested it with loggers and it generates the sitemap and it looks good.

But I have a problem.

This sitemap when I want to see it or use it from the web site already in publish is not shown.

If I go to mydomain.com/products-sitemap.xml

I get a 404 error that the file does not exist, what can I do?

I add the codes used

----------------------------------------------------------

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.sitemap.SitemapException;
import org.apache.sling.sitemap.builder.Sitemap;
import org.apache.sling.sitemap.spi.generator.SitemapGenerator;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.commons.Externalizer;
import com.day.cq.dam.commons.util.DamUtil;
import com.tfs.core.configuration.RetailSiteMapServiceImplConfig;

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

@Component(
    service = SitemapGenerator.class,
    property = {
        "service.ranking:Integer=20",
        "sitemapName=products-sitemap",
        "sitemap.generator.name=products-sitemap",
        "Name=products-sitemap",
        "Names=products-sitemap"
    }
)
@Designate(ocd = ReSiteMapServiceImplConfig.class)
public class ReSiteMapServiceImpl implements SitemapGenerator {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReSiteMapServiceImpl.class);
    private static final String SITEMAP_NAME = "products-sitemap";

    @Reference
    private Externalizer externalizer;

    private String publishUrl;

    @Activate
    @Modified
    protected void activate(ReSiteMapServiceImplConfig config) {
        this.publishUrl = config.publishUrl();
    }

    @Override
    @NotNull
    public Set<String> getNames(@NotNull Resource sitemapRoot) {
        return Collections.singleton(SITEMAP_NAME);
    }

    @Override
    public void generate(Resource sitemapRoot, String name, Sitemap sitemap, Context context) throws SitemapException {
     
        if (!SITEMAP_NAME.equals(name)) {
            LOGGER.info("Skipping sitemap generation. Name does not match: {}", name);
            return;
        }

        ResourceResolver resourceResolver = sitemapRoot.getResourceResolver();
        Resource productsRoot = resourceResolver.getResource("/content/dam/re/cf-products");

        if (productsRoot != null) {
            addProductsToSitemap(productsRoot, sitemap, resourceResolver);
        } else {
            LOGGER.warn("Products root not found");
        }

        LOGGER.info("Sitemap generation completed for: {}", name);
    }

    private void addProductsToSitemap(Resource root, Sitemap sitemap, ResourceResolver resolver) throws SitemapException {
        LOGGER.info("Adding products to sitemap from: {}", root.getPath());
        int productCount = 0;

        Iterator<Resource> children = root.listChildren();
        while (children.hasNext()) {
            Resource child = children.next();
            if (DamUtil.isAsset(child)) {
                String internalUrl = child.getPath().replace("/content/dam/re/cf-products", "/products") + ".html";
                String fullUrl = publishUrl + internalUrl;
                sitemap.addUrl(fullUrl);
                productCount++;
                LOGGER.debug("Added product to sitemap: {}", fullUrl);
            }
        }

        LOGGER.info("Total products added to sitemap: {}", productCount);
    }
}

----------------------------------------------------------
 # Rewrite for default sitemap request
RewriteCond %{REQUEST_URI} ^/sitemap.xml$ [NC]
RewriteRule ^/sitemap.xml$ /content/re/en/brand.sitemap.xml [PT,L]
RewriteRule ^/sitemap-index.xml$ /content/re/en/brand.sitemap-index.xml [PT,L]
RewriteRule ^/products-sitemap.xml$ /content/re/en/brand.products-sitemap.xml [PT,L]

----------------------------------------------------------

Best answer by EstebanBustamante

Hi, 

 

You are missing the "filter" configuration in the dispatcher to allow the request flow till you AEM Publish instance, please check here how to set this configuration: https://experienceleague.adobe.com/en/docs/experience-manager-learn/sites/seo/sitemaps#dispatcher-allow-filter-rule 

# Allow AEM sitemaps /0200 { /type "allow" /path "/content/*" /selectors '(sitemap-index|sitemap)' /extension "xml" }

 

Hope this helps

4 replies

kautuk_sahni
Employee
January 7, 2025

@aaron_dempwolff Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni
EstebanBustamante
EstebanBustamanteAccepted solution
New Participant
December 20, 2024

Hi, 

 

You are missing the "filter" configuration in the dispatcher to allow the request flow till you AEM Publish instance, please check here how to set this configuration: https://experienceleague.adobe.com/en/docs/experience-manager-learn/sites/seo/sitemaps#dispatcher-allow-filter-rule 

# Allow AEM sitemaps /0200 { /type "allow" /path "/content/*" /selectors '(sitemap-index|sitemap)' /extension "xml" }

 

Hope this helps

Esteban Bustamante
narendiran_ravi
New Participant
December 18, 2024

Hi @aaron_dempwolff , 

Did you allow the new selector products-sitemap in your dispatcher?

New Participant
December 19, 2024

Yes, I make a configuration but even with that doesn't work

/filters
{
/0001 { /type "allow" /url "/products-sitemap.xml" }
}

SreenivasBr
New Participant
December 22, 2024

This pattern seems incorrect. The correct pattern will be something like this:

 

/0100 { /type "allow" /method "GET" /path "/content/re/en/brand" /selectors "products-sitemap" /extension "xml"}