Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Duplicate page for locale pages while using sling:alias property

Avatar

Level 2

We have locale specific pages and we are using sling:alias property to have language specific page name . But the problem is both the urls are publicly accessible : 1) with sling:alias name 2) with original page name.

Example :

page is /content/brand/de/home.html

          -> This home page node has been set a sling:alias property of "zuhause"

Ideally end user should be seeing the page URL : /content/brand/de/zuhause.html

But currently both the urls are serving the content : /content/brand/de/home.html  and /content/brand/de/zuhause.html

How to block the original page url and serve only the locale specific (with sling:alias) url  ?

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Well, technically it's just an alias, so both ways are ok to use.

If I understand you correctly, you would like to have the page available only via it's alias, not with it's "real" name. You could implement a request filter, which checks if the requested resource is requested via it's real name and then do a redirect to its alias'ed name (or just return a 404 ...). Shouldn't be a big problem to implement that. And of course adapting the linkrewriter to only use the alias when building links.

On the other hand it find it always quite useful if I can just take the URL in the browser and look up that name in the content tree (CRXDE); that's getting harder if you only use aliases.

Jörg

View solution in original post

4 Replies

Avatar

Community Advisor

Hi,

You can block/redirect url either by server redirect or AEM sling:mapping.

You may also be interested to check URL Rewriting section at below:

SEO and URL Management Best Practices



Arun Patidar

Avatar

Correct answer by
Employee Advisor

Well, technically it's just an alias, so both ways are ok to use.

If I understand you correctly, you would like to have the page available only via it's alias, not with it's "real" name. You could implement a request filter, which checks if the requested resource is requested via it's real name and then do a redirect to its alias'ed name (or just return a 404 ...). Shouldn't be a big problem to implement that. And of course adapting the linkrewriter to only use the alias when building links.

On the other hand it find it always quite useful if I can just take the URL in the browser and look up that name in the content tree (CRXDE); that's getting harder if you only use aliases.

Jörg

Avatar

Level 2

Thanks Jörg !

I tried implementing a Custom Filter to achieve this functionality , but I am facing an issue : Request and Response objects are of type org.apache.sling.i18n.impl.I18NFilter$I18NSlingHttpServletRequest and they can not be cast to SlingHttpServletRequest and this request type is not an instance of SlingHttpServletRequest.

I tried ranking this custom filter higher than the CQ i18n filter but that is not helping.

@Service(Servlet.class)

@SlingFilter(

        label = "Custom Redirect Filter",

        description = "Custom Utility Redirect Filter",

        metatype = false,

        generateComponent = true,

        generateService = true,

        order = 100,

        scope = SlingFilterScope.REQUEST)

public class CustomRedirectFilter implements Filter {

    private static final org.slf4j.Logger log = LoggerFactory.getLogger(CustomRedirectFilter.class.getName());

    @Override

    public void init(FilterConfig filterConfig) throws ServletException {

       

    }

    @Override

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    if (!(request instanceof SlingHttpServletRequest)) {

chain.doFilter(request, response);

return;

}

  

    log.info("============================================================");

    log.info("Inside custom redirect filter");

    log.info("============================================================");

        final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;

        final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;

       

        final Resource resource = slingRequest.getResource();

        if(resource.getResourceType().equals("cq:Page"))

        {

         PageManager pageManager =  resource.getResourceResolver().adaptTo(PageManager.class);

         Page page = pageManager.getPage(resource.getPath());

         String templatePath =  page.getContentResource().getValueMap().get("cq:template").toString();

         if(templatePath.equals("/apps/custom/test-com/templates/default"))

         {

        slingResponse.setStatus(HttpServletResponse.SC_OK);

        slingResponse.setHeader("Location", "/content/test-com/en/error-404.html");

             slingResponse.setHeader("Connection", "close");

             return;

          }

        }

      

       chain.doFilter(slingRequest, slingResponse);

    }

    @Override

    public void destroy() {

      

    }

}

Avatar

Employee Advisor

The request and response object are inheriting from HttpServletRequestWrapper and HttpServletResponseWrapper which have a getResource() method.

Check:

sling-org-apache-sling-i18n/I18NFilter.java at 5db7d6143e16e6a9da27ccbb6c71165b0ea7f5e8 · apache/sli...