What are AEM Dispatcher rewrite rules? Our AEM Certified expert highlights the Top 5 advanced ways of using the Dispatcher for production and development.
Now that you know how to adjust the basic parameters of the dispatcher, it’s time to dive deep into the advanced settings. These settings will be necessary for you to fine-tune the production environment and will also be useful during the development process.
Below I’m describing a few useful tools for the advanced use of the dispatcher:
- static web servers tools
- rewrite module
- AEM tools for mappings
- repository mappings tree /etc/map
- ResourceProvider Interface
Rewrite module
You may enable the rewrite module by uncommenting the next line in your httpd.conf file:
LoadModule rewrite_module modules/mod_rewrite.so
Initial settings for the rewrite module may look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteLog "logs/rewrite.log"
RewriteLogLevel 9
# your rule 1
# your rule 2
…
# your rule n
</IfModule>
Here is a simple example of using the rewrite module:
RewriteRule ^/en(.*)\.html$ /content/geometrixx/en$1.html [R]
This rewrite rule provides external redirect.
Detailed and useful documentation: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Repository mappings tree /etc/map
Resource mapping is used to define redirects, vanity URLs, and virtual hosts for AEM:
- /etc/map – resource mappings tree;
- /etc/map/http – resource mappings for http requests.
Mappings definitions of your AEM instance are at: http://host:port/system/console/jcrresolver
Example: mapping that prefixes any request to http://localhost:4503 with /content prefix:
- Create node under /etc/map/http with type “sling:Mapping” with name “localhost_any”;
- Add properties to this node:
- Name “sling:match”, type “String”, value “localhost.4503/”;
- Name “sling:internalRedirect”, type “String”, value “/content/”.
This mapping will handle a request such as http://localhost:4503/geometrixx/en/products.html as if http://localhost:4503/content/geometrixx/en/products.html is requested. This example brokes requests to clientlibs and to other resources placed outside of /the content resource tree.
Detailed and useful documentation:
https://docs.adobe.com/docs/en/cq/5-6-1/deploying/resource_mapping.html
https://sling.apache.org/documentation/the-sling-engine/mappings-for-resource-resolution.html
ResourceProvider interface
You can use the ResourceProvider interface to create your own resource trees.
public interface ResourceProvider {
Resource getResource(ResourceResolver var1, String var2);
Iterator<Resource> listChildren(Resource var1);
}
You can set root paths for your implementation of the ResourceProvider:
@Properties({
@Property(
label = "Root paths",
description = "Root paths this Sling Resource Provider will respond to",
name = ResourceProvider.ROOTS,
value = {"/content/mount/samples"})
})
Detailed and useful documentation: https://sling.apache.org/documentation/the-sling-engine/resources.html
Summary
The three tools mentioned above provide additional enhancements when using the dispatcher:
- Httpd rewrite module;
- AEM mappings tree;
- ResourceProvider interface.
We have taken only a brief look at these tools. If you want to use them effectively, you need to learn the corresponding detailed documentation for each tool.
FAQ
How do you restart the dispatcher in AEM?
AEM Dispatcher is a module that runs on enterprise-class web servers. For example, on Apache httpd 2.4.x, the following command can be used to restart it: sudo /etc/init.d/apache2 restart.
What is the difference between rewrite and redirect?
URL manipulations such as rewrites and redirects can be used to hide a website’s internal structure, make URLs user-friendly, and handle legacy URLs. With rewriting, the server modifies the requested URL on-the-fly based on predefined rules before returning the content associated with the modified URL to the visitor. The original URL remains visible to the visitor. With redirecting, the server sends a response to the visitor’s browser instructing it to request a different URL. The browser then requests the new URL and the visitor sees the page associated with the new URL.
What is the ETC map in AEM?
The /etc/map/ folder in AEM is used to create definitions for internal resource mapping. For example, the /etc/map/http subfolder contains mappings for the HTTP protocol that allow defining redirects, vanity URLs, and virtual hosts for AEM.
How URL shortening is done in AEM?
URL shortening can be done using Sling Resource Resolver Factory if you need to use simple rules, such as removing numerous subfolders from the URL string or using Sling Mapping for more complex scenarios.
How does sling resolution work in AEM?
Apache Sling is used to match incoming request URLs to specific pieces of content in the JCR repository. The repository has a hierarchical structure and every piece of content in it has a long and unique resource path. To determine the matching content Apache Sling applies a set of predefined rules and resource type filters.