Persisted Selectors

What is Selector?

Apache Sling defines Selectors as: ” If the first character in the request URL after the resource path is a dot (.), the string after the dot up to but not including the last dot before the next slash character or the end of the request URL comprises the selectors. If the resource path spans the complete request URL no selectors exist. If only one dot follows the resource path before the end of the request URL or the next slash, also no selectors exist.”

Case Study

You might have used selectors many times in passing on parameters to a servlet or to identify different behaviors of a component using Sling Resource Resolution. Often there is a need to persist a selector of one page throughout the website which forces us to modify all existing links. Generally, such requests are handled by a JAVA class where all links are modified using that selector and the experience is persisted.

Another interesting approach to do so is using Cookie + Apache configurations if we don’t want to modify all the links on a website.Let’s say for one of your campaigns, there is a selector X on a page. Now based on that selector component behavior is changed and I need that experience to persist even if the user goes to multiple pages on the site and come to the first page.

When a user will land on that page you will set a Cookie (Let’s say “cookieX”) with that selector value.Do make sure you accept only valid Selector values and don’t create Cookie for any selector value. You can make this Cookie as either a Persisted (Will stay for a specified duration ) or Session (Will stay till the browser is Open) Cookie based on your use case and duration of the campaign.

Next, whenever this Cookie is present on the Client side, the following Apache Rule will persist this selector –

RewriteCond %{REQUEST_URI} !^/apps
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/home
RewriteCond %{REQUEST_URI} !^/libs
RewriteCond %{REQUEST_URI} !^/bin
RewriteCond %{REQUEST_URI} !^/tmp
RewriteCond %{REQUEST_URI} !^/var
RewriteCond %{REQUEST_URI} !(\.{1}[a-z]*)
RewriteCond %{HTTP:Cookie} cookieX=([^;]+) [NC]
RewriteRule ^/(.*)$ /$1.%1 [R=301]

Note: Assuming the initial request doesn’t have .html to it and the .html is appended after this Redirect Rule 

So for a request with

GET /content/geometrixx/en.html HTTP/1.1
Cookie: cookieX=xyz1234;

will Redirect to


Now as per the Rewrite Rule and duration of the Cookie life, your selector would be persisted and experience would sustain. You can always modify the Redirect Rule based on your use-case.

How to Read Selectors / Usage?


String selectors = slingRequest.getRequestPathInfo().getSelectorString();
String[] _selectors = request.getRequestPathInfo().getSelectors();



I hope this would make life simple while using Selectors in AEM and improve your caching.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s