Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you go about persisting selectors in links in CQ5 / AEM

Tags:

aem

sling

I am trying to keep particular selectors present within links when hitting a cq5 page.

For example, say you have gone to /content/mysite/mypage.stickyselector.html, I want all of my subsequent links on the page, such as the aboutus.html and contact.html pages to keep the aboutus.stickyselector.html and contact.stickyselector.html links.

There are a few reasons I'm trying to do this, including preventing excessive rewrites when mobile devices hit, such as mypage.smart.html, as we can let the rewrite rules allow the user to go through without redetection of device type, as well as any customised content etc.

I have tried creating my own Link Rewriting Transformer, which is fantastic for rewriting links where you have all of the information at hand, however, I don't seem to be able to obtain the selectors used to get to the page containing the links at this point.

Any help would be greatly appreciated.

like image 364
Bayani Portier Avatar asked Aug 15 '13 00:08

Bayani Portier


2 Answers

There are a couple of approaches:

  1. Rewrite the urls on the outbound response in CQ (as suggested by David)
  2. Rewrite the urls on the inbound request using mod_rewrite at dispatcher (assuming Apache)

As David covered scenario 1, I'll describe the second scenario

The "sticky" selector value can be returned in a cookie to the client

Set-Cookie: selector=stickyselector;

Each subsequent request to the site from the client will contain that cookie. You can then use that cookie to rewrite in the URL in apache before it gets presented to the dispatcher module (and eventually the publish instance:

RewriteCond %{HTTP:Cookie} selector=([^;]+) [NC]    # If we have the selector cookie
RewriteRule ^(.*)\.html$ /$1.%1.html [PT]           # add it to the request url before extension

So a request that arrives at the dispatcher looking like this:

GET /content/mysite/mypage.html HTTP/1.1
Cookie: selector=stickyselector;

Would arrive at the publish instance rewritten as:

/content/mysite/mypage.stickyselector.html

If you are using this approach for device/channel specific renditions then you could alternatively use the user agent value instead of a cookie to drive selector addition. For example:

RewriteCond %{HTTP_USER_AGENT} "iphone|ipod|iemobile" [NC]
RewriteRule ^(.*)\.html$ /$1.mobile.html [PT]              # add channel selector to the request url 

Positives of this approach are that all users are presented with the same URL (e.g./content/mysite/mypage.html) the selectors in URLs are only presented to CQ.

Negatives are that it normally would require cookies and it depends on apache configuration.

like image 127
diffa Avatar answered Oct 08 '22 00:10

diffa


If you have access to the sling request, you can get a string of all the selectors with:

String selectors = slingRequest.getRequestPathInfo().getSelectorString();
like image 38
David Gorsline Avatar answered Oct 08 '22 01:10

David Gorsline