How to use internal redirects in AEM?
We are migrating a Ruby on Rails based Application to Adobe Experience Manager, one of the requirements requested by the client was to take out the “content” word of the URL. We first though in using vanity URLs, but we would have to set them one by one in all the pages. So, we decided to use internal redirects for three main reasons:
1. They allow us to use regular expressions
2. We can keep the rules in our code base
3. We can set different rules for different domains
Creating an internal redirect is very easy. Here we are going to show how to create a very basic rule, if you require more information you can visit the [AEM page](https://docs.adobe.com/docs/en/aem/6-1/deploy/configuring/resource-mapping.html) about the topic or the [Apache Sling documentation](https://sling.apache.org/documentation/the-sling-engine/mappings-for-resource-resolution.html). We are also creating a second blog post to show how to use regular expressions and configure for different domains.
Creating the mapping in the authoring instance
To create the mapping rules, we should create them under the /etc/map/http node. We have to options to create the node, we could use the namespace as the name e.g. “super-domain.com” or we can use another custom name e.g. “localhost_any” in conjunction with the “sling:match” property. For this example, we will use the second approach and we are going to map the request for news.
1. Create a node under /etc/map/http with the name “localhost_any” and type “sling:Mapping”.
2. Click “Save All”
3. Add the following properties:
* “sling:match” with a value of “localhost.4502/news/” (it’s not a typo you should use “.” instead of “:”)
* “sling:internalRedirect” with a value of “/content/news/”
The above means we are going to catch all the request that “match” the “localhost:4502/news/” and we are going to redirect them internally to “/content/news/” so it will be transparent for the user, meaning that users will still see something like “localhost:4502/news/somepage.html” but we are going to be rendering “/content/news/somepage.html”.
To verify out mapping it’s working we can go to http://localhost:4502/system/console/jcrresolver and in the “Mapping Map Entries” at the bottom of the page we should be able to see our mapping.
Now if we go to the url: “http://localhost:4502/news/.html” it will work! We got rid of the “content” fragment in our URL.
Replicating for a publish instance
Now that we are sure our mapping is working on the authoring instance, let’s replicate it in the publish instance. For that we need to follow these steps:
1. Create a node under /etc called map.publish of type “sling:folder”
2. Create a node under /etc/map.publish called “http” of type “sling:folder”
3. Copy the “localhost_any” node from /etc/map/http to /etc/map.publish/http
4. Select the newly created /etc/map.publish folder, the select the “Replication” tab and press “Replicate”
*NOTE: For the replication to be successful we need our publish instance running.*
Now that we replicated our rules, we need to change some configuration in our publish instance, to do that:
1. Open http://localhost:4503/crx/de
2. Login as admin or a user with an administrator role (if not, you won’t be able to see the /etc/ folder).
3. Now you should be able to see the /ect/map.publish folder we replicated in our authoring instance, if not you may need to replicate the whole /etc folder. Select the /etc/map.publish/http/localhost_any node and change the value in “sling:match” from “localhost.4502/news” to “localhost.4503/news”. As you can see we changed the port to the one we are using in out publish instance (4503).
4. Copy the /etc/map.publish/http/localhost_any node to /etc/map/http/
5. Click “Save All”
Now we should be able to see our mapping being added to the system console: http://localhost:4503/system/console/jcrresolver
So, now we should be able to navigate to pages with the form “http://localhost:4502/news/yourpage.html”.
That’s it for now, thanks for reading!
Magmalabs.io is an expert UX/UI Design, Ecommerce, and Software Development consultancy. We build beautiful ecommerce, web, and mobile software solutions for startups, scale ups and corporate companies using innovative technologies, top talent, and exceptional quality assurance.