Main Content

Magnolia Community Forums: Get help with Magnolia: How to set a permanent redirect 301 http status in freemarker?


  • mathiaslin
    mathiaslin
    Full name: Mathias Conradt
    Posts: 29
    Last post: Jan 11, 2017 8:11:17 PM
    Registered on: Aug 17, 2015
    How to set a permanent redirect 301 http status in freemarker?
    #1 by mathiaslin on Sep 2, 2015 4:19:38 PM

    I want to to a 301 permanent redirect via Freemarker template.
    However, setting the status code is ignored, the redirect still always redirects with a 302 code ("temporarily moved").

    This is the code:


    ${ctx.response.setStatus(301)}
    ${ctx.response.sendRedirect("${content.externalLink}")}


    I know I could use VirtualURIMapping and the "permanent:" directive, but I need to redirect dynamically to editable URLs set by authors.

    (I'm on Magnolia 5.4.1 CE).

    Thanks.

  • breun
    breun
    Full name: Nils Breunese
    Posts: 416
    Last post: Feb 24, 2017 2:54:06 PM
    Registered on: Sep 16, 2010
    Re: How to set a permanent redirect 301 http status in freemarker?
    #2 by breun on Sep 3, 2015 11:44:56 AM

    VirtualURIMapping is an interface, so you can create your own implementation to handle dynamic redirects.

    You can also get a reference to the HttpServletResponse through MgnlContext.getWebContext().getResponse() and redirect by calling response.sendRedirect(redirectURL). I would recommend putting that logic in a model class instead of a template script.

    Nils.

  • pierre
    pierre
    Full name: Pierre Sandrin
    Posts: 1
    Last post: Jun 17, 2016 2:53:26 PM
    Re: How to set a permanent redirect 301 http status in freemarker?
    #3 by pierre on Jun 17, 2016 2:53:26 PM

    Hello there.

    Did you find a solution for this? It tried to achieve the same in a Model class. I think the response status must be set after the sendRedirect() method but it still does not work.

    This is my java code:

    final WebContext webContext = MgnlContext.getWebContext();
    webContext.getResponse().sendRedirect(link);
    webContext.getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);

    Best regards
    Pierre

  • mathiaslin
    mathiaslin
    Full name: Mathias Conradt
    Posts: 29
    Last post: Jan 11, 2017 8:11:17 PM
    Registered on: Aug 17, 2015
    Re: How to set a permanent redirect 301 http status in freemarker?
    #4 by mathiaslin on Jan 11, 2017 7:53:58 PM

    Hi Pierre,

    I did not find a solution yet but just coming back to this topic again today, looking further into it now.

    Regards,
    Mathias

  • mathiaslin
    mathiaslin
    Full name: Mathias Conradt
    Posts: 29
    Last post: Jan 11, 2017 8:11:17 PM
    Registered on: Aug 17, 2015
    Re: How to set a permanent redirect 301 http status in freemarker?
    #5 by mathiaslin on Jan 11, 2017 7:56:47 PM

    Hi Nils,

    thanks for the hint. I will look into it, however I would like to know why Magnolia would change the response code I am explicitly setting to a 302. What's the purpose of that in the first place?

    Regards,
    Mathias

  • mathiaslin
    mathiaslin
    Full name: Mathias Conradt
    Posts: 29
    Last post: Jan 11, 2017 8:11:17 PM
    Registered on: Aug 17, 2015
    Re: How to set a permanent redirect 301 http status in freemarker?
    #6 by mathiaslin on Jan 11, 2017 8:11:17 PM

    Hi Pierre,

    using DefaultVirtualURIMapping with "permanent:" prefix in the toURI works for me, it redirects with 301 status code now.



    Regards,

    Mathias

You don't have the permission to post on this thread

Sign in

To login on this forum, you can use your Magnolia Forge, Support or Partner account, or, below, your Google, Yahoo! or OpenID account. If you have trouble logging in, or any other sort of issue, please let us know in the Meta forum, on the user-list, or simply by email at forum-admin at magnolia-cms dot com.

* Required

... or sign in with:

  • icon http://{your-openid-url}
  • icon
  • icon https://me.yahoo.com/