Able to redirect all CMS pages that 404 to a different page?

I have a CMS collection of classes that a student could attend. I built a CMS collection template to display information about these classes.

Let’s say we have:

We use these links for various things like emails. However, we don’t have collection items for all of our classes (we are working on it but it will always lag). So, if a student clicks it will display a 404.

I’d like to have some generic page where if a collection item doesn’t exist, the link is redirected to that page instead of a 404.

For example, will show some generic page saying “Sorry we don’t have info on that class yet!”

Is this possible?

Yes it’s possible with a 404 redirect script that matches and redirects for specific URL patterns. In your case, the path pattern is /classes/(something).

Here’s a script I’ve created for that purpose, and a crude demo site.

Edit the redirects table, you only need one here, and it will look something like this. Change the redirect path to the page you want;

  var redirects = [{
    path: /^\/classes\/(.+)/gi,
    redirect: "/no-class-yet"

Here’s a CodePen with the script;
Instructions are in the markdown view in the pen.

Thank you, will try it out!