Basic URL Rewrite Rules | Hiding the Content Root from URL

One of the requirements in almost every AEM project is to set up the rewrite rules. A typical content URL follows below pattern:


It is a best practice not to reveal the repository structure and hide the top level path in the URL i.e. /content/mysite. This blog post will talk about the steps to be made to achieve this on both publish server and dispatcher.

Configuration on AEM Publish Server:

  • There are many ways to configure the rewrites on publish server. e.g. Resource Resolver, Vanity URLs, Sling mapping. Configuring the Apache Sling Resource Resolver Factory is one of the ways to configure rewrites on publish server. This service needs to be configured on the publish server so that if someone hits the publish server directly, we see the correct rules applied there.
    Make sure that the publish server has the below configuration appliedApache sling resource resolver factory
  • Once you apply this configuration, wait for 5 minutes so that all the services are up and running.

Configuration required on Dispatcher:

  • Set DispatcherUseProcessedURL property to 1. i.e.

<IfModule disp_apache2.c>
DispatcherConfig dispatcher.any
DispatcherLog /var/log/apache2/dispatcher.log
DispatcherLogLevel 3
# DispatcherNoServerHeader 0
DispatcherDeclineRoot 0
DispatcherUseProcessedURL 1
DispatcherPassError 0
# DispatcherKeepAliveTimeout 60

  • Make sure the mod_rewrite module is loaded in apache.
  • Update the virtual host file and add the below rules in it.

<IfModule mod_rewrite.c>
#Enable this to debug the redirect logs
LogLevel alert rewrite:info
RewriteEngine on

# remove any trailing slash, if it's there.
RewriteRule ^(.+)/$ $1

#Shorten the URL
RewriteRule ^/content/mysite/(.*).html$ $1.html [R,L]
#Map the root folder to the home page
RewriteRule ^/?$ en.html [R,L]

# Ignore requests to "known" AEM root paths, and prefix all others with the proper AEM prefix
RewriteCond %{REQUEST_URI} !^/apps
RewriteCond %{REQUEST_URI} !^/content
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/home
RewriteCond %{REQUEST_URI} !^/libs
RewriteCond %{REQUEST_URI} !^/system
RewriteCond %{REQUEST_URI} !^/tmp
RewriteCond %{REQUEST_URI} !^/var
RewriteRule ^/(.*)$ /content/mysite/$1 [PT,L]



Troubleshooting Tips:

  • Restart your apache server every time you make any changes.
  • Enable logging for rewrite module. It will help you to monitor the rules getting applied at each level
  • Check dispatcher logs whether the rules are applied or not and which request is going to publish server.
  • Check if the page invalidation is happening correctly after applying the rewrite rules.
  • Check all the links within the page and navigation bar are correctly shortened.

Hope it helps !! 🙂