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:

'/some-old-slug' => '/new-slug'

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

Create the blueprint

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

title: Redirect
    display: Main
        handle: redirect_url
          characterlimit: 0
          type: text
          localizable: false
          display: 'Redirect URL'
          validate: required

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:

{{ redirect to="{{ redirect_url }}" }}

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:

title: Redirects
route: '{slug}'
layout: redirect
  - redirect
revisions: false
sort_dir: desc

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:

title: RSS
entry: 65ede336-9f41-4b20-bae3-cd3cd9435d24
updatedby: 197c1509-8dff-4d72-9898-334084519619
updatedat: 1578797584
redirect_url: /feed
id: f358bf66-3972-4800-864f-aa42eeb7aee4

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