Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

RedirectTarget Property Issue || .html extension issue

arvind
Level 4
Level 4

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
Arun_Patidar
Correct answer by
Community Advisor
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]

View solution in original post

4 Replies
davidjgonzalezz
Employee
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();
        }
    }
}

 

arvind
Level 4
Level 4

@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.

davidjgonzalezz
Employee
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.
Arun_Patidar
Correct answer by
Community Advisor
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]

View solution in original post