3
votes

I can see broadly two approaches for URL rewriting in AEM:

  1. Sling mapping (sling:Mapping) under /etc/map/http(s)
  2. URL rewriting using link rewriter/TransformerFactory.

I want to know which one is better among two - in sense of ease of implementation, scalability, maintenance & automation.

Regards.

2
anything else apart from two approaches above?Finn
One more option at dispatcher level - mod_rewrite. Comparison is broad topic, probably fellow SO members can suggest.Sandeep Kumar
with mod_rewrite only, we may undermine AEM/sling features - both are a combo.Finn

2 Answers

8
votes

there are always multiple options to a problem in Sling. If you look at the topic "URL rewriting" it has two dimensions:

  • outbound - e.g. shorten links /content/path/en/about.html to /en/about/
  • inbound - e.g. map an inbound request from /en/about/ to a resoure request /content/path/en/about.html

Outbound: URL rewriting is usually be done outbound by a LinkRewriter/TransformerFactory. In theory, you could also change your component to render differently or change your content. But that's not recommended. To apply a Transformer you can use

  • /etc/map mapping (recommended), referred to as Mapping Map Entries [1]
    • enhanced mapping allowing for complex rules, also for regex-based rules
    • allows for different mapping per domain or protocol
    • can ensure complete externalization of links
  • ResourceResolver Map Entries [1]
    • traditional mapping, very simple rules only
    • Does not take domain or protocol into account
    • requires resolver restart on change (can be expensive for large production environments)
  • Custom TransformerFactory
    • full power to change all links on the way out based on Sax+custom rules

Inbound: Your inbound requests can be rewritten or mapped on Sling or at infrastructure levels before (Apache HTTPD mod_rewrite or CDN s.a. Akamai)

  • Apache HTTPD mod_rewrite (recommended for production) - modify the request before it gets forwarded to the Dispatcher module. Recommended as it allows for enhanced security as well as for proper and simple caching and de-caching rules
  • Sling - usually not for production, as caching might become difficult
    • /etc/map
    • ResourceResolver
    • RequestFilter [2]
    • NonExistingResource servlet
  • CDN: same as mod_rewrite. Inbound manipulation before the request reaches the Dispatcher

HTH

[1] https://docs.adobe.com/docs/en/aem/6-2/deploy/configuring/resource-mapping.html

[2] https://sling.apache.org/documentation/the-sling-engine/filters.html

[3] https://sling.apache.org/documentation/the-sling-engine/mappings-for-resource-resolution.html

5
votes

This depends on which rewriting are you referring to. Inbound or Outbound.

When it comes to Inbound rewriting I'd advise to use mod_rewrite and just properly rewrite your content there with a single rule - this is quite efficient.

When it comes to Outbound rewriting (handling links in your html) you should definitely go with Sling Mappings - as they are more efficient and clear - and they are designed just for this purpose.

Take a look at this blog which explains the whole rewriting journey: https://www.cognifide.com/our-blogs/cq/multidomain-cq-mappings-and-apache-configuration/