Home > Mobile >  How to redirect a route from :id to :slug
How to redirect a route from :id to :slug

Time:12-16

I had a route that initially called all blog posts by id, and now I have converted it to using the slug as the rendered URL. In my oversight, I did not think that existing links to my site would crash my app. How can I make it so that any request for my blog that was routing as /blog/:id to now route as /blog/:slug?

CodePudding user response:

I think what you need is a single middleware (or if statement) as Lawrence Cherone mentioned in the comment.

Inside that middleware identify whether the parameter is id or slug. if it is an id, then do a database query find the slug and return res.redirect('/blog/${slug}');

  • Related