April 27, 2020
Estimated Post Reading Time ~

How to implement extension-less URL's in AEM

As per the SEO best practices, it is better to define extensionless URL's to boost the ranking, AEM requires the extension to understand and serve the incoming request.

This post explains the approach to achieve the extensionless URL in Adobe Experience Manager(AEM)

There are two steps
- Rule Configuration Dispatcher
Remove .html extension from incoming URL with /
Append the .html while invoking the publisher for the URL's ending with /

- AEM etc/map configuration
Reverse mapping to rewrite the html URL in the pages to extensionless
Forward mapping to map the incoming request to the resource

This is tested in AEM 6.2 version

Apache configurations:
#Handle the landing page
RewriteRule ^/$ /en/ [R=301,L]
#Mask the /content/geometrixx-outdoors path
RewriteRule ^/content/geometrixx-outdoors/(.*)(\.html)?$ /$1 [NE,L,R=301]

#Replace the .html with /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^/(.*).html$ /$1/ [R=301,L,QSA]

#Append the .html for those URL's ending with / before sending to publisher
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^/(.*)/$ /$1.html [PT,L,QSA]

Publisher etc/map configurations:
Create a node localhost.8080(replace with required DNS and port) of type sling:Mapping under /etc/map/http or /etc/map/https based on the protocol used
Add the following property

sling:internalRedirect[] - /content/geometrixx-outdoors

<?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:Mapping"
sling:internalRedirect="[/content/geometrixx-outdoors]">
<redirect/>
<reverse/>
</jcr:root>




Create a node "redirect" of type sling:Mapping under localhost.8080 - map the incoming request to AEM resource path

Add the below properties

sling:internalRedirect[] - /content/geometrixx-outdoors/$1 , /$1
sling:match - (.+)$

<?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:Mapping"
sling:internalRedirect="[/content/geometrixx-outdoors/$1,/$1]"
sling:match="(.+)$"/>




This will help to perform the forward mapping of URL to the resource by a sling, resourceResolver.resolve() API can be used if a programmatic mapping is required

Create a node reverse of type sling:Mapping under localhost.4502 - Reverse mapping, map the content path to shortened and extensionless URL

sling:internalRedirect[] - /content/geometrixx-outdoors/(.*).html
sling:match - $1/

<?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:Mapping"
sling:internalRedirect="[/content/geometrixx-outdoors/(.*).html]"
sling:match="$1/"/>




This will help the rewriter to perform the Reverse mapping of the html links in the pages to shortened and extensionless URLs.
The output rewriter pipeline automatically rewrites all the html links in the pages to shortened and extensionless URLs based on the above configuration, resourceResolver.map() API can be used if a programmatic mapping is required

Now the browser displays the extensionless URL for the html page request and also the html links inside the page is converted to extensionless



The extensions from the page links can be stripped off by enabling the "Strip HTML Extension" configuration in "Day CQ Link Checker Transformer" also but

- This configuration is not site-specific and applied for all the sites hosted
- In AEM 6.3 I was facing too many redirect issue by enabling this configuration along with above mentioned Apache rules but the same is working fine in AEM 6.2
- This will not append the / at the end of rewritten URLs





By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.