Spesific Child Pages & dynamic cannonical



Hello im trying to implements 2 things:

1. im trying to implement lang on particular page, every children pages will get this lang into 'efg' but the parent get lang="en"

what i did: lang="<${currentPage.path == '/content/path1/path2/parent(/.*)?'>" (not worked)

so i put manually every path of this children one by one, like "<${currentPage.path == '/content/path1/path2/parent/child1' || '/content/path1/path2/parent/child2"... ? 'efg' : 'en'

is there any method that can fix it? so i only need to call it once?


2. im trying tomake cannonical URL base on dynamic url

for example: <link rel="cannonical" href="${currentPage.path}"> and the outputs return "/content/path1/path2/parent"

how to get real url like "www.example.com/path1/path2/parent/file.html"


Thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)



Hi @iwanttobeDev 


1. To solve your first issue, you can try below HTL generic code, which will be true for its all first-level children.

${currentPage.parent.path == '/content/path1/path2/parent'}


2. Go through this link https://stackoverflow.com/questions/19571249/get-current-page-url-in-adobe-cq5-for-canonical-link




Answers (3)

Answers (3)



I think it is bad idea to use to string manipulation to do the URL shortening as AEM is a enterprise level CMS and you can never restrict the URL length. Please use the either/combination of options mentioned in below post along with externalizer to get the absolute paths. The same logic will also help you in shortening url in page content using link rewrites in future. I would love to use Sling Resolution along with Server level redirects to best use of the implementations.





Actually i found a little tricky for this canonnical,for example:

<link rel="cannonical" href="https://www.example.co.id{$data.path}">

and on js i put:
data.pathh = currentPage.path.substr(13);
it will eliminate : '/content/test'

so it will return 'https://www.example.co.id/yourdinamycal/path.html"

thanks for any of your help.




For both of the things to work, please write a sling model based on Page and init() post construct method, put the logic of getting the current page and get the language properties and set the calculated language attribute into a gettermethod, please follow the same for second scenario as well.


In you sightly, you should simply write.

<sly data-sly-use.pagemodel="yourpackage.core.models.PageModel"/>


PS:- Please refrain from writing any backend logic in Sightly