Forum Discussion

Henrik's avatar
Henrik
Advisor
11 years ago

Is it possible to make a redirect with REST or Freemarker?

Hi,

 

I’d like to make a redirect from server side, before the page loads.

Is it possible to do that?

 

I have tested the http.response. setRedirectUrl("<redirect url>") object but it doesn’t work.

 

Thank you for the help!

Henrik

  • Hi Henrik,

     

    The http.response.setRedirectUrl("<redirect url") call only works from a special freemarker Page Initialization script that runs once each request, early-on in the request (before any of the html is rendered) and is there to allow for things like redirects.  Traditionally, professional services has added this for customers, but we have exposed this through Lithium Studio as a beta feature.  If you contact support and reference this Lithosphere post, they should be able to enable the tab (called "Page Initalization") that lets you edit this template.

     

    -Doug

7 Replies

  • DougS's avatar
    DougS
    Khoros Oracle
    11 years ago

    Hi Henrik,

     

    The http.response.setRedirectUrl("<redirect url") call only works from a special freemarker Page Initialization script that runs once each request, early-on in the request (before any of the html is rendered) and is there to allow for things like redirects.  Traditionally, professional services has added this for customers, but we have exposed this through Lithium Studio as a beta feature.  If you contact support and reference this Lithosphere post, they should be able to enable the tab (called "Page Initalization") that lets you edit this template.

     

    -Doug

  • Henrik's avatar
    Henrik
    Advisor
    11 years ago

    Hi Doug,

     

    By the way, is there any documentation on that studio tab?

    Can we use freemarker in this section? And does this feature apply to all the apges of the community?

     

    Thanks a lot!

  • DougS's avatar
    DougS
    Khoros Oracle
    11 years ago

    We are working on a TKB article that will explain how to use the new tab.  In the meantime, I can describe it to you here: it's a tab with one textarea that lets you place freemarker into it that will run once on every request.  This runs on the server, and does not render any markup, so don't try placing any markup in there -- it won't render anything if you do.  

     

    Here is an example script you might add -- for this example, let's say you want all anonymous users who hit the community page to be redirected to a custom page you've added through Lithium Studio (and also want to make sure that all signed-in users who go to your anonymous user landing page get redirected to the actual community page).  The freemarker markup you would add might look like this:

     

    <#if user.anonymous && page.name == "CommunityPage">
       ${http.response.setRedirect(webuisupport.urls.page.name.get("custom-anonymous-user-landing-page").build())}
    <#elseif !user.anonymous && page.name == "custom-anonymous-user-landing-page">
     ${http.response.setRedirect(community.urls.frontPage)}
    </#if>

     

  • iftomkins's avatar
    iftomkins
    Maven
    11 years ago

    Doug, thank you SO much. If you could, please make sure that your project managers know about this new feature. I wish we had been notified. We've gone back and forth constructing an SOW for a change we were told that only Lithium could do. Now we have  easily completed that change via the Page Init tab. Thanks again!