Petr Shypila
Petr Shypila

Reputation: 1499

Adobe CQ. Getting current page path

I need to set current page path to canocical tag. Unfortunately I have some bugs here. On this code:

<%
ResourceResolver resolver = slingRequest.getResourceResolver();
Externalizer externalizer = resolver.adaptTo(Externalizer.class);
String canonUrl = externalizer.publishLink(resourceResolver, "http", currentPage.getPath());
%>
<link rel="canonical" href="${canonUrl}" />

I have this output:

<link rel="canonical" href="" />

What is wrong. But if I setting canonical tag like this:

<link rel="canonical" href="${currentPage.path}" />

I have this output:

<link rel="canonical" href="/content/example/eu/germany/de_de/about.html" />

That's almost fine. And now, all I need it's just to add domain name(http://example.com/content/example/eu/germany/de_de/about.html). But how I can do this without hardcoding? Approach should works fine also on localized pages.

Upvotes: 0

Views: 7967

Answers (2)

Pakira
Pakira

Reputation: 2021

you can read host from a bean class through jstl -

private String getHost(HttpServletRequest request)
  {
    String xHost = request.getHeader("x-forwarded-host");
    if(xHost == null)
    {
        String host = request.getHeader("Host");
        if(host == null)
        {
            String urlS = request.getRequestURL().toString();
            try
            {
                return (new URI(urlS)).getAuthority();
            }
            catch(URISyntaxException use)
            {
                return (new 
              StringBuilder()).append(request.getServerName())
               .append(":").append(request.getServerPort()).toString();
            }
        } else
        {
            return host;
        }
       }
       if(xHost.indexOf(',') >= 0)
        xHost = xHost.substring(xHost.indexOf(','));
    return xHost.trim();
   }

Upvotes: 1

santiagozky
santiagozky

Reputation: 2539

You need to configure the domain in the Externalizer Service. Go to the OSGi console (/system/console/components) and look for com.day.cq.commons.impl.ExternalizerImpl. Then click on configure and add lines for local, author and publish. For example:

local http://localhost:4502
author http://author.example.com
publish http://publish.domain.com

Upvotes: 3

Related Questions