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