Expand my Community achievements bar.

Sitemap custom cannot be accessed on my AEM website

Avatar

Level 5

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]

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

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

2 Replies