AEM Dispatcher Part-0
This blog will clear doubts related to dispatcher rewrite rule, condition, shortening, etc. As part of this blog we will mainly talk about DISPATCHER URL’s cosmetic changes.
This blog will also show us the clear difference between dispatcher rewrite rules, conditions etc. and ResourceResolverFactory configurations which is use for shorten URL’s.
Redirect to a specific URL or short URL after hitting in the browser is happen because of dispatcher rewrite rules and conditions.
Whatever shorten URL we see as part of chrome inspect element comes because of ResourceResolverFactory configurations.
Before jumping on this session, please click on this link and read about dispatcher and how to do initial setup in local. This link will allow you to setup dispatcher in local within 5–10 minutes if your publish environment is up.
RewriteRule and RewriteCondition
Lets make below changes to add RewriteEngine, RewriteEngine, RewriteCond, etc.
- remove # to uncomment below line for enabling reqrite module in httpd.conf file as shown below.
This mod_rewrite module will allow us to rewrite, shorten, cosmetics changes in the URL.
2. Open httpd.conf file and add below lines at line 179 or just before <IfModule !mpm_netware_module>:
Code Snippet to copy paste in httpd.conf file:
<IfModule mod_rewrite.c>
<VirtualHost *:80>
ServerName localhost
ServerAlias practice.abc
LimitRequestFieldSize 32768
AllowEncodedSlashes NoDecode
RewriteEngine On
RewriteRule ^/?$ /content/practice/us/en/test.html [PT,L]
RewriteRule ^/content/practice/(.*)$ /$1 [NE,L,R=301]
RewriteCond %{REQUEST_URI} !^/apps
RewriteCond %{REQUEST_URI} !^/bin
RewriteCond %{REQUEST_URI} !^/content
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/home
RewriteCond %{REQUEST_URI} !^/libs
RewriteCond %{REQUEST_URI} !^/saml_login
RewriteCond %{REQUEST_URI} !^/system
RewriteCond %{REQUEST_URI} !^/tmp
RewriteCond %{REQUEST_URI} !^/var
RewriteCond %{REQUEST_URI} (.html|.jpe?g|.png|.svg|.gif|.GIF)$
RewriteRule ^/(.*)$ /content/practice/$1 [PT,L]
</VirtualHost>
</IfModule>
Explanation:
Below line allow us to load test.html page if we hit localhost in the URL. Internally it will call make URL to load data http://localhost/content/practice/us/en/test.html
RewriteRule ^/?$ /content/practice/us/en/test.html [PT,L]
Below line allow us to load http://localhost/content/practice/us/en/test.html internally if we hit http://localhost/us/en/test.html in the browser.
RewriteRule ^/(.*)$ /content/practice/$1 [PT,L]
Hitting this http://localhost/content/practice/us/en/test.html URL will automatically redirect to http://localhost/us/en/test.html
RewriteRule ^/content/practice/(.*)$ /$1 [NE,L,R=301]
3. Set dispatcherUseProcessed URL to on
DispatcherUseProcessedURL on
Follow below steps to add domain name as practice.abc in local windows host file:
- Traverse to below folder hierarchy: c:\Windows\System32\Drivers\etc\hosts
2. Open hosts file and make below highlighted entry in the file:
3. In httpd.conf file add ServerAlias as practice.abc
4. Now if we hit practice.abc in brower, it will load practice.abc/content/practice/us/en/test.html page internally.
5. Now if we hit practice.abc/us/en/test.html in browser, it will load practice.abc/content/practice/us/en/test.html page internally.
ResourceResolverFactory Mapping
As we are hitting shorten URL and at the same time able to load page. Now, Go back to our test.html page, inspect element this page will show all URL’s are coming as full or complete URL starting from /content.
We will be using ResourceResolverFactory mapping configuration to show below highlighted URL as shorten URL /us/en/offer
Paste below configurations as part of org.apache.sling.jcr.resource.internal.JcrResourceResolverFactoryImpl.xml file inside config.publish hierarchy as we want to shorten URL in publish mode only.
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
resource.resolver.map.location="/etc/map.publish"
resource.resolver.mapping="[/content/practice/</,/:/]"
/>
Create button component having link field as shown below:
<div data-sly-use.button="com.javadoubts.core.models.ButtonModel">
<a href="${button.mappedLink}">
<button>Click Me</button>
</a>
</div>
Create ButtonModel.java sling model class to shorten link property URL from /content/practice/us/en/home to /us/en/home
OUTPUT:
After successful build, it will show below shorten anchor link URL:
Note: Similar thing we can perform with DAM URL also as /content/dam/practice/first.png to /assets/first.png
Resource Resolver Console Configuration
Traverse to Resource Resolver web console using link helps us to test mapping configurations.
It will help us to Resolve and Map input URL’s.
User /content/practice/us/en URL to resolve and map.
Resolve
It will show type and other info if provided URL gets successfully resolved. It will also show Mapping location as /etc/map
Map
Click on Map button will show mapped URL as /us/en of /content/practice/us/en provided URL.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.