Posted on under Statamic by Owen Conti.
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:
1'/some-old-slug' => '/new-slug'
We can do this using Statamic's
redirect
tag. The premise is essentially:
redirect
tag in itFirst, we need to create the blueprint. Here's what I am using:
1title: Redirect 2sections: 3 main: 4 display: Main 5 fields: 6 - 7 handle: redirect_location 8 field: 9 characterlimit: 010 type: text11 localizable: false12 display: 'Redirect URL'13 validate: required
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
:
1{{ redirect to="{{ redirect_location }}" }}
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:
1title: Redirects2route: '{slug}'3layout: redirect4blueprints:5 - redirect6revisions: false7sort_dir: desc
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
:
1 2--- 3 4title: RSS 5entry: 65ede336-9f41-4b20-bae3-cd3cd9435d24 6updatedby: 197c1509-8dff-4d72-9898-334084519619 7updatedat: 1578797584 8redirect_location: /feed 9id: f358bf66-3972-4800-864f-aa42eeb7aee410 11---
Test it out: /rss should take you to /feed
Hopefully you found this article useful! If you did, share it on X!
Found an issue with the article? Submit your edits against the repository.