Handling 301 redirects and vanity URL creation in Contentful

What is the best way to manage the creation of 301 redirects and the creation of vanity URLs in Contentful? I am new to the platform, but it feels like there should be a widget. How is everyone managing these without having dev. do them each time?


1 Like

Contentful isn’t directly involved in the routing of webpages so it really depends on the frontend you’ve built to work with Contentful to determine how to do routing and use vanity URLs. That being said, it is definitely possible to define navigation content types or redirect content types within Contentful so that those are controllable by editors using Contentful, but ultimately it would depend on a bit of frontend development to make it work.

I was hoping to find some useful tips here I could use myself to improve what I already have, but no such luck. Ah well.

I can however provide my own basic tips though on how I did something similar.

For the URL’s, I used a “Slug” text field in my content type (you’ll find that as a display option for the single line textbox field type). These are auto generated from the entry title, but can be overwritten too, and look something like “my-nice-url”. Then in your code you can query based on the slug value, grab the first entry in the array (unless you specifically need 2nd, 3rd, etc) then boom, you got it.

As for 301, 302, 410, etc, that’s a little trickier since Contentful only has statuses Published, Draft, or Archived. Which to me is 200, 404, and ??? (whatever you want). Due to the way we were handling fetching entries I had to do quite a bit of reworking to allow querying of archived items, but basically I now consider anything that is archived to be a 410. You could do something similar for 301, but then you’d have to programatically control where the redirect goes to, which you may not be able to do easily. So it might be easier to have perhaps 2 fields on your content type, redirect type (permanent/temporary) and a link to the new item, or url, or whatever. Then in your code you can determine what to do if these values are populated. (Most like either query Contentful again to get the values from the ID of the linked item, and deliver the appropriate status code to the user, or just redirect if your value is a URL string.)

It can be quite annoying though if you want lots of different content types to be “redirectable”, which is fine for us since we only have 1 content type for now, but it’s still annoying, but unfortunately I couldn’t find a better way to handle it unless the Contentful team add a redirect link into their end which you could then set and query.

Hope this at least gives you some ideas to try out :slight_smile:

As an aside, this is a classic symptom of Contentful being used in a way that you would normally expect from something like Umbraco, Wordpress, or Sitecore to power a website.

There are inevitable tradeoffs to using decoupled platforms and this is one of them. There’s just no way to provide any sort of universal solution as it will be the webserver running the end-application doing the redirection (or earlier if using Lambda@Edge, etc) and Contentful has no knowledge of that part of the setup…

Also from a technical POV, querying a third-party API to determine if a page should be redirected is really bad practice…