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 

8 Replies

  • SeanA's avatar
    SeanA
    Lithium Alumni (Retired)
    11 years ago

    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)
    11 years ago

    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
    11 years ago

    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
    11 years ago

    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>

  • HaidongG's avatar
    HaidongG
    Lithium Alumni (Retired)
    11 years ago

    Hi iftomkins,

     

    Great code! Thanks a lot for sharing, it definitely helps other people in the future.

     

    there is one little part inside the code can be improved for performance reason. (in bold and red color)

     

                 <#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 />
    <#break /> </#if>

     we don't need to continue looping once is_user_admin is set to true. 

     

    Regards

    /H

  • iftomkins's avatar
    iftomkins
    Maven
    11 years ago

    I'm attempting to trigger a similar redirect on the Error Page. The reason is that a link to a forum that doesn't exist was included in an app, and we want to redirect users so they don't get a broken link. Here is the redirect code in my page initialization tab:

     

    <#-- Error page redirects -->
    <#if page.name == "ErrorPage">
        <#assign currentUrl = http.request.url />
        <#if currentUrl?contains("/t5/MobileTrack/bd-p/mobiletrack") >
            ${http.response.setRedirectUrl("/t5/iOS-App/bd-p/ios")}
        </#if>
    </#if>

  • rwm's avatar
    rwm
    Advisor
    10 years ago

    Be aware that the profile language may include a region specifier.   For example, our community uses "es-mx" and "pt-br".  So you would want to either look at the first two characters only with:

     

    <#assign currentLanguageRedirect = rest("/users/id/${user.id}/settings/name/profile.language").value?substring(0,2)>

     

    ..or just test for the full value with:

     

    <#case "es-mx">