Asset Redirection | Community
Skip to main content
Leela-Pavan-Kumar
New Participant
August 4, 2022
Solved

Asset Redirection

  • August 4, 2022
  • 1 reply
  • 1468 views

I have a requirement where if we hit for 'X' asset it has to redirect to 'Y' asset.

 

How to implement this redirection in AEM.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

@leela-pavan-kumar, in that case you can use one of below solutions:

  • Apache rewrite map - https://httpd.apache.org/docs/current/rewrite/rewritemap.html
  • Redirect Map Manager form ACS Commons - https://adobe-consulting-services.github.io/acs-aem-commons/features/redirect-map-manager/index.html
  • Finally you can write your own Sling Filter. Below is very simple example, that will redirect you from /document/adobe.pdf to /content/dam/wknd/en/adobefile.pdf. I have tested it on clear AEM 6.5.13 - and it works without any issue. Of course you will need to manage list of redirect somehow, but there are many options, e.g. CA Configuration.
    package com.example.filters;
    
    import org.apache.sling.api.SlingHttpServletRequest;
    import org.apache.sling.api.SlingHttpServletResponse;
    import org.apache.sling.api.servlets.HttpConstants;
    import org.apache.sling.engine.EngineConstants;
    import org.osgi.framework.Constants;
    import org.osgi.service.component.annotations.Component;
    
    import javax.servlet.*;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    @Component(service = Filter.class, immediate = true, property = {
            EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
            EngineConstants.SLING_FILTER_METHODS + "=" + HttpConstants.METHOD_GET,
            Constants.SERVICE_RANKING + "=" + Integer.MAX_VALUE
    })
    public class RedirectFilter implements Filter {
    
        private Map<String, String> redirectMap = new HashMap<String, String>();
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            redirectMap.put("/document/adobe.pdf", "/content/dam/wknd/en/adobefile.pdf");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) servletRequest;
            SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) servletResponse;
    
            String path = slingRequest.getRequestPathInfo().getResourcePath();
            if (redirectMap.containsKey(slingRequest.getRequestPathInfo().getResourcePath())) {
                slingResponse.sendRedirect(redirectMap.get(path));
                return;
            }
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
        }
    }
    

1 reply

lukasz-m
New Participant
August 18, 2022

Hi @leela-pavan-kumar,

You can achieve this using Redirect Manager from ACS Commons:

I did a quick test on AEM 6.5.13 and ACS Commons 5.3.2, and it worked.

This is how my test configuration looks like:

So when I hit:

  • localhost:4503/content/dam/we-retail/en/experiences/48-hours-of-wilderness/48-hours-of-wilderness-1.jpg

as a result I am getting:

  • localhost:4503/content/dam/we-retail/en/experiences/skitouring/skitouring-1.jpg
Leela-Pavan-Kumar
New Participant
August 18, 2022

Tired this one @lukasz-m , but here the source needs to be there in our DAM Assets.

 

In our case Asset is not present in our DAM.

 

if we hit

    localhost:4503/document/adobe.pdf

it has to take me to 

    localhost:4503/content/dam/wknd/en/adobefile.pdf

   

lukasz-m
lukasz-mAccepted solution
New Participant
August 18, 2022

@leela-pavan-kumar, in that case you can use one of below solutions:

  • Apache rewrite map - https://httpd.apache.org/docs/current/rewrite/rewritemap.html
  • Redirect Map Manager form ACS Commons - https://adobe-consulting-services.github.io/acs-aem-commons/features/redirect-map-manager/index.html
  • Finally you can write your own Sling Filter. Below is very simple example, that will redirect you from /document/adobe.pdf to /content/dam/wknd/en/adobefile.pdf. I have tested it on clear AEM 6.5.13 - and it works without any issue. Of course you will need to manage list of redirect somehow, but there are many options, e.g. CA Configuration.
    package com.example.filters;
    
    import org.apache.sling.api.SlingHttpServletRequest;
    import org.apache.sling.api.SlingHttpServletResponse;
    import org.apache.sling.api.servlets.HttpConstants;
    import org.apache.sling.engine.EngineConstants;
    import org.osgi.framework.Constants;
    import org.osgi.service.component.annotations.Component;
    
    import javax.servlet.*;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    @Component(service = Filter.class, immediate = true, property = {
            EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,
            EngineConstants.SLING_FILTER_METHODS + "=" + HttpConstants.METHOD_GET,
            Constants.SERVICE_RANKING + "=" + Integer.MAX_VALUE
    })
    public class RedirectFilter implements Filter {
    
        private Map<String, String> redirectMap = new HashMap<String, String>();
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            redirectMap.put("/document/adobe.pdf", "/content/dam/wknd/en/adobefile.pdf");
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) servletRequest;
            SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) servletResponse;
    
            String path = slingRequest.getRequestPathInfo().getResourcePath();
            if (redirectMap.containsKey(slingRequest.getRequestPathInfo().getResourcePath())) {
                slingResponse.sendRedirect(redirectMap.get(path));
                return;
            }
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
        }
    }