Created
December 29, 2018 11:21
-
-
Save sgaem/e5588e272513477e99017dc4fe9d9e77 to your computer and use it in GitHub Desktop.
Redirect.jsp for 301 and 302 redirection in AEM 6.2 and AEM 6.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@page session="false" | |
import="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/><% | |
String location = properties.get("redirectTarget", ""); | |
String type = properties.get("redirectType", ""); | |
// 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)) ) { | |
// 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"; | |
} | |
if (type.equals("301")) { | |
response.setStatus(301); | |
response.setHeader("Location",redirectPath); | |
} else { | |
response.sendRedirect(redirectPath); | |
} | |
} else { | |
response.sendError(HttpServletResponse.SC_NOT_FOUND); | |
} | |
return; | |
} | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment