AEM

How to use internal redirects in AEM? (Part 2)


Specifying the domain

As we saw earlier, in the first part of this post series, we can specify the matching URL of our mappings with the sling:match attribute e.g. “mydomain.com/news”. We can also specify the domain outside the sling:match attribute by using it as the node’s name.

CRXDE_Lite

As we can see in the image, now we just put “news” in the matches attribute and omit the domain. Sling will take the node’s name add use it to add the rest of the URL. So the mapping in the example will look for the URL matching the following: “mydomain.com/news” and make an internal redirect to “mydomain.com/content/news.html”.

But, what if you have several rules that you want to apply for the same domain?. Well, in that case, you can create a sling:Folder type node with the domain as the name, and create the mapping nodes inside. it would look like:

CRXDE_Lite_1

Now you will be able to group mapping for different domains.

Using regular expressions

Let’s suppose we are asked to hide the “content” segment on the URL as well as the “.html” extension on all the pages under “news”. One option is to create a mapping for each one of the pages, but as the number of pages grow, maintainability will start to be an issue.

The best way to handle that situation is to use regular expressions. So, the solution is to use a regular expression in the sling:match with a capture group like this: “news/(.+)” so we can get the name of the page. Then, in the sling:InternalRedirect attribute we need to interpolate the page name by getting the content of the capture group as follows: “/content/news/$1.html”.

CRXDE_Lite_2

One difference between using and not using regular expressions in the mappings is that they are added under the “Resolver Map Entries” section in the system console instead of the “Mapping Map Entries” section and I will look like:

Adobe_Experience_Manager_Web_Console_-_Resource_Resolver

For more information about this topic, check the Apache Sling documentation.

That’s it for now, thanks for reading!

AEM
Responsive layouting in AEM
AEM
How to use internal redirects in AEM?