Expand my Community achievements bar.

internal redirect in AEM 6.2

Avatar

Level 1

Hi,

I am using AEM 6.2 and while creating a page i m setting its advanced Property of redirect to another Page. But this is not happening. the resourceSuperType being used is foundation/components/page. Redirect code in page.jsp is as following:

      <%@page session="false"

            contentType="text/html; charset=utf-8"

            import="com.day.cq.commons.Doctype,

                    com.day.cq.wcm.api.WCMMode,

                    com.day.cq.wcm.foundation.ELEvaluator" %><%

%><%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%

%><cq:defineObjects/><%

    // read the redirect target from the 'page properties' and perform the

    // redirect if WCM is disabled.

    String location = properties.get("redirectTarget", "");

    // resolve variables in path

    location = ELEvaluator.evaluate(location, slingRequest, pageContext);

    boolean wcmModeIsDisabled = WCMMode.fromRequest(request) == WCMMode.DISABLED;

    boolean wcmModeIsPreview = WCMMode.fromRequest(request) == WCMMode.PREVIEW;

    if ( (location.length() > 0) && ((wcmModeIsDisabled) || (wcmModeIsPreview)) ) {

        // check for recursion

        if (currentPage != null && !location.equals(currentPage.getPath()) && location.length() > 0) {

            // check for absolute path

            final int protocolIndex = location.indexOf(":/");

            final int queryIndex = location.indexOf('?');

            String redirectPath;

            if ( protocolIndex > -1 && (queryIndex == -1 || queryIndex > protocolIndex) ) {

                redirectPath = location;

            } else {

                redirectPath = slingRequest.getResourceResolver().map(request, location) + ".html";

            }

            // include wcmmode=disabled to redirected url if original request also had that parameter

            if (wcmModeIsDisabled) {

                redirectPath += ((redirectPath.indexOf('?') == -1) ? '?' : '&') + "wcmmode=disabled";

            }

            response.sendRedirect(redirectPath);

        } else {

            response.sendError(HttpServletResponse.SC_NOT_FOUND);

        }

        return;

    }

    // set doctype

    if (currentDesign != null) {

        currentDesign.getDoctype(currentStyle).toRequest(request);

    }

%><%= Doctype.fromRequest(request).getDeclaration() %>

<html <%= wcmModeIsPreview ? "class=\"preview\"" : ""%>>

<cq:include script="head.jsp"/>

<cq:include script="body.jsp"/>

</html>

i have tried to use foundation/components/redirect, but this is also not working. Can you help me with workaround or a solution for this?

1 Reply

Avatar

Level 8

Check below code :

You need to call this in body.html of your page

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.ValueMap;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.adobe.cq.sightly.SightlyWCMMode;

import com.adobe.cq.sightly.WCMUsePojo;

import com.day.cq.wcm.api.Page;

public class RedirectHandler extends WCMUsePojo {

    private static final Logger LOG = LoggerFactory.getLogger(RedirectHandler.class);

    public RedirectHandler() {

        super();

        LOG.info("Entry into RedirectHandler constructor");

    }

    private ValueMap properties;

    private SlingHttpServletResponse response;

    private SightlyWCMMode wcmMode;

    private Page currentPage;

    @Override

    public void activate() {

        properties = getProperties();

        wcmMode = getWcmMode();

        currentPage = getCurrentPage();

        response = getResponse();

    }

    public void getConfigureResponse() {

        String redirectPath = properties.get("customRedirectTarget", "");

        if ((redirectPath !=  "" )&& ((wcmMode.isDisabled()) || (wcmMode.isPreview()))) {

            if (currentPage != null && !redirectPath.equals(currentPage.getPath())) {

                response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);

                response.setHeader("Location", redirectPath + ".html");

                response.setHeader("Connection", "close");

            } else {

                try {

                    response.sendError(HttpServletResponse.SC_NOT_FOUND);

                } catch (IOException e) {

                    LOG.error("IOException",e);

                }

            }

            return;

        }

    }

}

http://keysandstrokes.info/aem-301-redirect-handler/