Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

RedirectTarget Property Issue || .html extension issue

Avatar

Level 6

Dear Team,

 

We have two pages lets say /content/A and /content/B .

 

A page redirects to Page B using page property redirect Target. so when we try to access page A, it redirects to page B, which is fine.

 

Problem: when we hit https://<domain>/content/A in browser, It redirects to   https://<domain>/content/B.html.

Is there any way to remove this html extension.

 

NOTE: we have already written webserver rules so that when we hit any page without .html extension, It works fine.

 

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You need to rewrite rule at webserver when you request /content/A.html it removes .html and redirect request to /content/A.

Request forwarding will not work.

 

e.g. 

RewriteRule ^/(.*).html$ /$1 [R=301,NE,L]


Arun Patidar

View solution in original post

4 Replies

Avatar

Employee

I assume you're using Core Components Page component.

 

It looks like you're running into: 

 

https://github.com/adobe/aem-core-wcm-components/blob/master/bundles/core/src/main/java/com/adobe/cq...


...based on the above code, you could add a vanity URL to the target page that is the page URLe with minus the ".html" and it looks like that should work.

 

If you wanted to write come code, you could overlay the Core Components Page's "redirect.html" script, and provide your own redirect target resolution, for example:

(This may need some syntax correction/tweaks - I wrote it in the forum post so its not exact but should give you the idea :))

 

Overlay: /apps/..../components/my-page/redirect.html

 

<template data-sly-template.redirect="${ @ redirectTarget}">
    <p 
        data-sly-use.myCustomRedirect="com.example.blah.MyCustomRedirect.class @ redirectTargetPage = redirectTarget.page">
    
    class="cmp-page__redirect">${'This page redirects to' @ i18n}
        <a href="${myCustomRedirect.getURL}">${redirectTarget.page.title || myCustomRedirect.getURL}</a>
    </p>
</template>

 

com.example.blah.impl.MyCustomRedirectImpl.java (and make corresponding SlingModel interface MyCustomRedirect.java)

@Model(adaptables = SlingHttpServletRequest.class, adapters = {MyCustomRedirect.class})
public class MyCustomRedirectImpl implements MyCustomRedirect {

    @RequestAttribute // I think this is right injector for the params passed in from HTL... if not, you can use the catchall @inject (but better to figure out the right injector)
    private com.adobe.cq.wcm.core.components.models.Page redirectTargetPage;

    @Override
    public String getURL() {
        NavigationItem navigationItem = redirectTargetPage.getRedirectTarget();

        if ( /** logic to determine if .html should be removed from redirect ** /) {
         return StringUtils.removeEnd(navigationItem.getUrl(), ".html"));
        } else {
            return navigationItem.getUrl();
        }
    }
}

 

Avatar

Level 6

@davidjgonzalezz ,

 

Thank you for your reply.

 

Unfortunately,It does not work as expected.

 

Below are the two points:

 

1) Vanity url solution suggested by you still add ".html" in url in case of redirectTarget.

2) Regarding overlay of redirect.html, I think this is used for below purpose i.e. when we open page in editor  mode and page is having redirectTarget Property value. 

e.g.

redirect.png

Thank you once again.

Avatar

Employee
ah - youre right. sorry about that - maybe that code is actually buried in the cq/Page impl. I dont see anything in the Core Components project that looks like it performs that actual redirect itself.

Avatar

Correct answer by
Community Advisor

You need to rewrite rule at webserver when you request /content/A.html it removes .html and redirect request to /content/A.

Request forwarding will not work.

 

e.g. 

RewriteRule ^/(.*).html$ /$1 [R=301,NE,L]


Arun Patidar