Redirect from slug to date/slug

Hi everyone! I recently migrated from an rather old Ghost instance to a more recent one, and I realize few days after, that the new blog was using /slug URL directly instead of /year/month/day/slug.

I made the fix in the route file with now permalink: /{year}/{month}/{day}/{slug}/ so it’s OK. However, I did made a post before the change, and now I have a 404 if I use the URL without the date.

I took a look at the redirect YAML file, and in the doc there’s an example to go from date to slug directly:

301:
  domain.com/blog/year/month/day/post-slug/: domain.com/post-slug/

However, I’d like to do the opposite:

301:
  domain.com/post-slug/: domain.com/blog/year/month/day/post-slug/

But it seems to create redirect loop. What would be the best option? Until then, I made a rewrite in Nginx, but I would prefer to keep things in Ghost.

See the later example that uses regex. ^ means start of the line, and avoids redirect loops!

Hey thanks for your answer!

Could you give me a quick example? I’m not entirely sure to get it.

In my case, going from https://xcp-ng.org/blog/xcp-ng-8-3-beta-2/ to https://xcp-ng.org/blog/2024/02/15/xcp-ng-8-3-beta-2/

Oh huh. I’m not sure there’s a generic way to get year, month, and day into a generic redirect rule on the target side. (Hey @RyanF, if there is, it’d be nice to have it in the docs!)

Since it sounds like there’s only one post impacted, here’s how to deal with it:

^\/blog\/xcp-ng-8-3-beta-2\/: /blog/2024/02/15/xcp-ng-8-3-beta-2/
1 Like

Thanks, tested and it worked. Curious to see if there’s a general case though :slight_smile: (waiting for @RyanF 's advice)

1 Like

AFAIK, there isn’t a programmatic way to get date params in the link for redirects (like there is with routes, e.g. {year}).

Agreed, redirects are (currently) context-free, since there’s no way to know if you’re trying to do an (e.g.) affiliate redirect, or something like this.