Setting up dynamic HTTP redirects with Statamic v3

Published under Statamic.

You can use Statamic's redirect tag plus a custom blueprint to setup dynamic HTTP redirects on your Statamic website.

Let's say you're building a CMS for a client, and they need the ability to create redirects on the fly, eg:

We can do this using Statamic's redirect tag. The premise is essentially:

  • Create a blueprint that has a single field which we'll input our destination URL into

  • We create a new layout file that only has a redirect tag in it

  • Create a collection to store the redirects for the site

  • The slug of each redirect entry will be the "from" URL

Create the blueprint

First, we need to create the blueprint. Here's what I am using:

Create the layout file

Next, we need to create a new layout file. The location of this file depends on your site setup, but for me, the file goes in resources/views/redirect.antlers.html:

Create the collection

Finally, we need to create the collection for the redirect entries. Feel free to use the Control Panel to do this. I'll post my collection's YAML file for reference:

Add your redirects!

That's it! Now you can create as many redirects as your heart desires! Here's a file showing how I redirect /rss to /feed:

Test it out: /rss should take you to /feed

Thanks for reading!

Did you find this post useful? Let me know on Twitter! If you found an issue with the content, submit a pull request!

Subscribe to my newsletter to know when I publish more content in the future.

❤️ Likes: 0
📣 Retweets: 0
💬 Replies: 0
🙊 Mentions: 0