Skip to content

Redirects using freemarker in JBake

When we started converting the old Apache Shiro website from SCMS to JBake, I noticed some of the sites were just redirects. Luckily, it is quite easy to do similar things in JBake using Freemarker.

I have not found a solution to modify .htaccess using freemarker, so this is the solution I came up with.

Creating a new template

The first things you need to do is to create a new template. First, create a file templates/redirect.ftl with these contents:

That is a barebone. Extend it as you wish (e.g. a delay in the javascript and nicer messages).

Adjusting jbake.properties

Now Jbake needs to know about the new template. You just need to add a single line:

Creating redirect content

Ok, the last step in the chain is to create a site which actually redirects the user to the target site. I want to redirect quickstart.html (generated from quickstart.adoc) to 10-minute-tutorial.html.

The new asciidoc attribute :jbake-target: /10-minute-tutorial.html is not optional. Hence, I did not define a default, so the baking will fail if the attribute is missing. This is intentional.

Now, everytime someone opens quickstart.html, he will be redirected to /10-minute-tutorial.html.

Conclusion

Creating a redirect only takes three simple steps.
Let me know if you found this small tutorial helpful.

Published inHow Tos

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *