Expand my Community achievements bar.

SOLVED

core components page 301 redirect

Avatar

Level 4

Hello!

 

We are migrating our client from 6.4 to 6.5 and also converting static templates to dynamic templates. We are using v3 of the Page component provided by core wcm components. With this page core component, we can set the redirect target in the page properties under advance tab. It works fine but we have a requirement to change the redirect code to 301 (Moved Permanently). How do we go about this implementing it? 

 

The search for 301 redirects in the experience league community brings several results, but the solutions they provide seem to be more fitted for foundation page component (not the core component, I might be wrong though). 

 

Any help/pointes you can provide will be greatly appreciated!

 

Thanks,

SkmAtl 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

hi @skmAem ,

 

Could you please try below approach 

if (properties.containsKey("redirectTarget")) {
            String redirectTarget = properties.get("redirectTarget", String.class);
            response.setStatus(Integer.parseInt(properties.get("redirectStatusCode", "301")));
            response.setHeader("Location", redirectTarget);
        } else {
            // Render your page normally
        }

 

This code checks for the "redirectStatusCode" property on the current page and sets the HTTP response code to 301 if it exists. If the "redirectTarget" property exists, it sets the "Location" header on the HTTP response to the value of that property.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

hi @skmAem ,

 

Could you please try below approach 

if (properties.containsKey("redirectTarget")) {
            String redirectTarget = properties.get("redirectTarget", String.class);
            response.setStatus(Integer.parseInt(properties.get("redirectStatusCode", "301")));
            response.setHeader("Location", redirectTarget);
        } else {
            // Render your page normally
        }

 

This code checks for the "redirectStatusCode" property on the current page and sets the HTTP response code to 301 if it exists. If the "redirectTarget" property exists, it sets the "Location" header on the HTTP response to the value of that property.