Search code examples
seoaemcanonical-link

Get Current Page Url In Adobe CQ5 for Canonical Link


Is there a standard way in Adobe CQ5 to get the current page url in order to create automatic canonical links in the head of the JSP?

Basically we have a dialogue config box where you can override the canonical link, but by default if this is empty I want to construct the link based on the raw URL path for the current request.

The JSP for the head is shared across all requests.

Many thanks


Solution

  • The ${currentPage.path} variable will give you the path of the parent page to the current resource, but you should use the Externalizer to make this into a nice user-facing URL, (i.e. applying any mapping rules that you have in place & using the correct domain name, rather than the server name e.g. prod-server-123)

    i.e.

    ResourceResolver resourceResolver = request.getResourceResolver();
    Externalizer externalizer = resourceResolver.adaptTo(Externalizer.class);
    String canonicalUrl = externalizer.publishLink(resourceResolver, "http", currentPage.getPath());
    

    You can customise what the externalizer treats as the base URL via Felix, or via an osgi:Config node in your repository. and read more on the official docs on the Adobe site.

    (Note that the Externalizer is CQ5.5+)