Ad Code

Saturday, December 29, 2018

Implement 301 and 302 Redirect in AEM



Hello Everyone,

A website redirect will take one website URL and point it to another. When anyone
types in or clicks on that original URL they’ll be taken to the new page or website.

There are few different types of redirects.
301 Redirect:A 301 redirect is a permanent redirect from one URL to another. 301
redirects send site visitors and search engines to a different URL than the one they
originally typed into their browser or selected from a search engine results page.
The big reasons marketers might set up a 301 redirect are:
  • To rebrand or rename a website with a different URL.
  • To direct traffic to a website from other URLs owned by the same organization.
Note:- In case of 301 redirect browser cache the mapping of new URL with old URL.

302 Redirect:A 302 status code means Found, or more commonly referred to as “temporarily moved.” This redirect doesn’t carry or pass the link value to the new location. What it does do is get the user to an appropriate location for you so that you aren’t showing them a broken link, a 404 page not found, or an error page.
Note:- In case of 302 redirect browser does not maintain any mapping or cache. So, server receives hit for both the URLs.
In AEM, OOTB redirect URLs do 302 Redirect.Redirect URL will only work in disabled and publish mode but not in edit mode.
Fig-1 Redirect Widget in Page Properties Dialog
So How to do 301/302 Redirect in AEM?
Add a custom widget as drop down to select 301 and 301 redirect and value must be 301 and 302 only.
Fig-2 Redirect Type Option in Page Properties

Let’s start with AEM 6.2/AEM 6.3 with 301/302 Redirect

The logic of redirection is written in /libs/foundation/components/page/page.jsp file so need to seperate the redirection logic in one another jsp file may be “redirect.jsp” but logic in this jsp only do 302 redirect so let’s add logic for 301 redirect.and include that jsp in header.html. 
The implementation of 301 and 302 Redirection is exactly same in AEM 6.2 and AEM 6.3

redirect.jsp having the logic for 301 and 302 redirection:

Demonstration Video on 301 and 302 Redirection in AEM 6.2


AEM 6.4 with 301/302 Redirect:
In AEM 6.4, AEM has provided a feature of redirecting a page by default.It means no need to write
any redirect.jsp for 302 Redirection.


So how 302 Redirect is working in AEM 6.4?
My observation: I created a page component without any inheritance and created a
page configured a redirect path in it.
While rendering a page this jsp "/libs/cq/Page/Page.jsp" gets called. This jsp
includes proxy.jsp and that proxy.jsp is calling a servlet com.day.cq.wcm.foundation.impl.PageRedirectServlet.java with selector "redirect".
Fig-3 : Call PageRedirectServlet from proxy.jsp

com.day.cq.wcm.foundation.impl.PageRedirectServlet.java class exists in the bundle shown below.
Fig-4: Bundle that has the class PageRedirectServlet.java
Now the question come how to configure 301 in AEM 6.4??
To achieve 301 redirect,
  • Overlay /libs/cq/Page hierarchy and make a servlet with a different selector and make an entry of that selector in proxy.jsp.So that always your servlet will get called (in which the logic for 301 and 302 redirection is written ) in place of OOTB servlet.
  • Make your own servlet class with the same selector(redirect) with 301 redirect logic in it and make the service ranking of this servlet higher.OOTB Servlet is not using any service ranking, so you can create servlet using service ranking and then always your servlet will get called.
Fig-5: Servlet Resolver Console that shows the scripts running for redirection as per preference
Custom PageRedirectServlet
Demonstration Video on 301 and 302 Redirection in AEM 6.4

If you have any query or suggestion then kindly comment or mail us at

sgaem.blog02@gmail.com
Hope it will help you guys !!
Thanks and Happy Learning.

5 comments:

  1. How did you got to the cq/libs/Page jsp ? Wasn't able to get there on my own : (

    ReplyDelete
  2. Nicely explained. Thanks for shareing.

    ReplyDelete
  3. Nice example. I was looking for exactly same thing. Thanks:)

    ReplyDelete
  4. Great walk through! Does this approach also work for AEM as a Cloud Service?

    ReplyDelete
  5. just to correct you 301 is permanent and 302 is temporary. In your video, you have revered both of these

    ReplyDelete