AEM Cloud Service - Dispatcher Mod Substitute and Mod Rewrite for URL Shortening and Link Rewriting

Goal

For URL Shortening (eg. shorten /content/eaem-url-rewrite-apache-substitute/us/en/landing.html to /landing.html ) or Link Rewriting needs, there are features available in AEM like Apache Sling Resource Resolver Factory (http://localhost:4502/system/console/configMgr/org.apache.sling.jcr.resource.internal.JcrResourceResolverFactoryImpl), /etc/map, resolver.map(String resourcePath), resolver.resolve(String incomingPath) etc. 

However, arguably, Link Rewriting and Path Resolving is probably done best at dispatcher using the included mod_substitute and mod_rewrite modules (when it comes to path matching and redirects configuration, Dispatcher might be a clear winner over AEM)

     1) Use AddOutputFilterByType SUBSTITUTE text/html and shorten the links...

     2) Expand the links when sending requests to AEM using RewriteRule

Demo | Package Install | Github



Solution

1) Create a sample project using AEM archetype...

mvn -B org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate -D archetypeGroupId=com.adobe.aem -D archetypeArtifactId=aem-project-archetype -D archetypeVersion=36 -D aemVersion="cloud" -D appTitle="Experience AEM URL Rewrite Subsitute" -D appId="eaem-url-rewrite-apache-substitute" -D groupId="apps.eaem.sites" -D frontendModule=none -D includeExamples=n -D includeDispatcherConfig=y


2) Copy eaem-url-rewrite-apache-substitute\dispatcher\src\conf.d\available_vhosts\default.vhost and create eaem-url-rewrite-apache-substitute\dispatcher\src\conf.d\available_vhosts\eaem.vhost 


3) Add the following in eaem-url-rewrite-apache-substitute\dispatcher\src\conf.d\available_vhosts\eaem.vhost. The following directive substitutes any text patterns (or outgoing links in response body) starting with /content/eaem-url-rewrite-apache-substitute/us/en/ and ending with .html with just the page relative path. eg. /content/eaem-url-rewrite-apache-substitute/us/en/products/software/aem.html with /products/software/aem.html

<Location "/">
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|/content/eaem-url-rewrite-apache-substitute/us/en/(.+?).html|/$1.html|"
</Location>


4) Create a symlink eaem-url-rewrite-apache-substitute\dispatcher\src\conf.d\enabled_vhosts\eaem.vhost pointing to eaem-url-rewrite-apache-substitute\dispatcher\src\conf.d\available_vhosts\eaem.vhost using the following command (windows)


mklink eaem.vhost "../available_vhosts/eaem.vhost"


5) Add the following configuration in eaem-url-rewrite-apache-substitute\dispatcher\src\conf.d\rewrites\rewrite.rules. This adds the path removed in step 3, back to incoming requests before sending to AEM eg. converts /products/software/aem.html to /content/eaem-url-rewrite-apache-substitute/us/en/products/software/aem.html so AEM knows where to find the page resource...

RewriteCond %{REQUEST_URI} (.html|.jpe?g|.png|.svg)$
RewriteRule ^/(.*)$ /content/eaem-url-rewrite-apache-substitute/us/en/$1 [PT,L]


6) From dispatcher_httpdaccess_xxxx.log

cm-p10961-e880305-aem-publish-7655759b6d-htvsx 107.204.55.186 - 22/Mar/2023:17:49:30 +0000 "GET /landing.html HTTP/1.1" 200 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"

cm-p10961-e880305-aem-publish-7655759b6d-htvsx 107.204.55.186 - 22/Mar/2023:17:49:34 +0000 "GET /dispatcher-substitute.html HTTP/1.1" 200 827 "https://publish-p10961-e880305.adobeaemcloud.com/landing.html" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"

6) From dispatcher_aemdispatcher_xxxx.log

[22/Mar/2023:22:09:16 +0000] [I] [cm-p10961-e880305-aem-publish-7655759b6d-htvsx] "GET /content/eaem-url-rewrite-apache-substitute/us/en/landing.html" - 0ms [publishfarm/-] [actionhit] publish-p10961-e880305.adobeaemcloud.com
[22/Mar/2023:22:09:18 +0000] [I] [cm-p10961-e880305-aem-publish-7655759b6d-htvsx] "GET /content/eaem-url-rewrite-apache-substitute/us/en/dispatcher-substitute.html" - 0ms [publishfarm/-] [actionhit] publish-p10961-e880305.adobeaemcloud.com

No comments:

Post a Comment