Forum Discussion

iftomkins's avatar
11 years ago

How to implement url redirects in Freemarker?

Hi,

 

I've researched and found what worked for some people to achieve URL redirects with freemarker. However, it's not working for me.

 

The redirect portion looks like this: ${response.sendRedirect(response.encodeRedirectURL("/t5/Deutsch/ct-p/DE"))} 

 

Everything else in the code seems to be working well, since the console.logs I added show that the redirect code is running, it's just not redirecting the user. How would I do this? Thanks in advance!

 

Here is my code:

 

<#-- Check if the user is an admin -->
<script>console.log('got here 1');</script>
<#assign is_user_admin = false />
<#if user.registered >
<script>console.log('got here 2');</script>
<#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>
<#if role.name?? && (role.name == "Administrator" || role.name == "Moderator" || role.name == "Moderator2"|| role.name == "Analytics" )>
<#assign is_user_admin = true />
<script>console.log('got here 3');</script>
</#if>
</#list>
</#if>

<#-- If the user is not an admin, get the language setting from their profile and store it in a variable -->
<#if !is_user_admin >
<#assign currentLanguageRedirect = rest("/users/id/${user.id}/settings/name/profile.language").value>
<script>console.log('got here 4');</script>
<#switch currentLanguageRedirect>
<#case "de">
${response.sendRedirect(response.encodeRedirectURL("/t5/Deutsch/ct-p/DE"))}
<#break>
<#case "es">
${response.sendRedirect(response.encodeRedirectURL("/t5/Español/ct-p/SP"))}
<script>console.log('got here 5');</script>
<#break>
<#case "fr">
${response.sendRedirect(response.encodeRedirectURL("/t5/French/ct-p/FR"))}
<#break>
<#case "ja">
${response.sendRedirect(response.encodeRedirectURL("/t5/日本語/ct-p/JP"))}
<#break>
<#case "ko">
${response.sendRedirect(response.encodeRedirectURL("/t5/한국어/ct-p/KR"))}
<#break>
<#case "ch">
${response.sendRedirect(response.encodeRedirectURL("/t5/繁體中文/ct-p/CH"))}
<#break>
<#default>
${response.sendRedirect(response.encodeRedirectURL("/t5/Help-Forums/ct-p/product"))}
</#switch>
<script>console.log('got here 6');</script>
</#if>

 

  • Hi iftomkins, 

    The http.response.setRedirectUrl("<redirect url") call only works from a special freemarker Page Initialization script, you can't use that with your normal components. please check out this post 

  • SeanA's avatar
    SeanA
    Lithium Alumni (Retired)

    I'm not certain but I suspect your redirect code is executing "too late." I've run into this in lives prior to Lithium as well. Where is your code? If it's in a custom component I believe that's too late. 

     

    Lithium provides a script designed specifically to address this. If that sounds like the problem, check this out.

  • HaidongG's avatar
    HaidongG
    Lithium Alumni (Retired)

    Hi iftomkins, 

    The http.response.setRedirectUrl("<redirect url") call only works from a special freemarker Page Initialization script, you can't use that with your normal components. please check out this post 

    • iftomkins's avatar
      iftomkins
      Maven

      Great--thanks so much!

       

      As suggested in the post you linked to, I just reached out to Lithium support to give us those permissions. We were told by our Project Manager, as recently as two weeks ago, that this was something only Lithium could do (and we would have to pay a lot of money for), so that's nice that they recently have made this available. Go Lithium!

      • iftomkins's avatar
        iftomkins
        Maven

        Thanks again! Here is the final code, which redirects users away from the Root page to their proper category page, based on language:


        <#-- Description: If user is not an Admin/Mod, redirect them away from root to the proper language page  -->
          <#if page.name?lower_case == "mobilecommunitypage" || page.name?lower_case == "communitypage" >
              <#-- Check if the user is an admin -->
              <#assign is_user_admin = false />
              <#if user.registered >
                     <#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>
                         <#if role.name?? && (role.name == "Administrator" || role.name == "Moderator" || role.name == "Moderator2"|| role.name == "Analytics" )>
                             <#assign is_user_admin = true />
                         </#if>
                     </#list>
               </#if>
              <#-- If the user is not an admin, get the language setting from their profile and store it in a variable -->
              <#if !is_user_admin >
                 <#assign currentLanguageRedirect = rest("/users/id/${user.id}/settings/name/profile.language").value>
                   <#switch currentLanguageRedirect>
                      <#case "de">
                                ${http.response.setRedirectUrl("/t5/Community/ct-p/DE")}
                        <#break>
                      <#case "es">
                                ${http.response.setRedirectUrl("/t5/Comunidad/ct-p/SP")}
                         <#break>
                      <#case "fr">
                                 ${http.response.setRedirectUrl("/t5/Communauté/ct-p/FR")}
                         <#break>
                      <#case "ja">
                                 ${http.response.setRedirectUrl("/t5/コミュニティ/ct-p/JP")}
                         <#break>
                      <#case "ko">}
                                 ${http.response.setRedirectUrl("/t5/커뮤니티/ct-p/KR")}
                         <#break>
                      <#case "ch">
                                ${http.response.setRedirectUrl("/t5/社区/ct-p/CH")}
                        <#break>
                      <#default>
                                ${http.response.setRedirectUrl("/t5/Help-Forums/ct-p/product")}
                   </#switch>
                   <#-- Run Set user language script, unless we set doNotSetUserLanguage to true here  -->
                   <#assign doNotSetUserLanguage = true />
              </#if>
          </#if>