Support Forums

Netlify redirects regex

Hi, Netlify team!

We have an issue on our project. We have 2 paths like: /directory/[uid] and /directory/page-[pageNumber].
When we go to /directory/[uid] we need to show one page (for uid template), and when we go to /directory/page-[pageNumber] we need to show the same page as for /directory/index, but the path should be the /directory/page-2 for example.
It’s easy to do with next rewrites in next.config.js, because I can use regex there. But I can’t find the way to describe the same logic of rewrites in netlify.toml. I understand that the easiest way is to make something like directory?page=2, but it’s not accepted by our SEO team.

Is it possible to do it somehow?

Hi @alisakova,

I’m not sure where the redirect destination is in your case.

Show one page, but does that page exist somewhere or basically how are you rendering it?

It might be possible to use placeholders in redirects like:

/directory/:uid /destination/foo/:uid 301!

But yeah, for that there must exist a destination.

About the second one, I don’t think simple redirects can handle it, you’d have to make use of Netlify Functions to handle it.

So, you might do something like:

/directory/* id=:id /.netlify/functions/redirect/ 301!

This will match the URLs with the path:

/directory/foo/?id=1 and pass that ID to the serverless function. In that, you’d have to generate the HTML page and serve it.

There’s no easy way to do this and you’d probably be better off with query string parameters like you’ve already suggested, but if that’s a hard no, then this would be the way forward.