_redirects but only if date in URL

I’m deploying Jekyll to Netlify and my blog posts have a pretty typical URL structure of domain.com/YEAR/MONTH/SLUG

My plan is to remove the /year/month portion of some of my posts, but I want to make sure that the old URLs work.

I saw the part of the documentation where placeholders can be used in the origin and target paths, but I’m worried about how this will be handled with pages where the first thing after the domain is NOT a year.

For example, I do want to redirect

to

https://dangerouslyawesome.com/i-don’t-follow-coworking-in-the-news

but I do not want to redirect an origin URL that is

Does that make sense? Is there a way to check and make sure that the url is only being redirected if it starts with a year (4 digit number)?

Our redirects are not that fancy, unfortunately (fancy enough to recognize 4 digits or even 4 characters versus /anythingofanylength/).

If you had them already in a subdirectory like:

/posts/

you could redirect /posts/*, but since you don’t, we need a different pattern.

I’m not sure how far back your posts go in date, but this pattern might work, repeated an appropriate number of times:

/2019/:month/:slug /:slug 302!
/2018/:month/:slug /:slug 302!

and so forth. I use 302’s to test with and if they work, change to 301’s.

cf https://www.netlify.com/docs/redirects/#placeholders

Ah, checking based on the year is a great workaround that I can make work with just the years. Thank you!

1 Like