Next.js Discord

Discord Forum

Next13 App router: Redirect to new url with new dynamic slug string

Unanswered
Himalayan posted this in #help-forum
Open in Discord
Avatar
HimalayanOP
I have structure like this "products/[topic]/[slug]"
How can i redirect from path : products/tools/12 ----> products/work/hammer
i changed url with new readable slug naming and old links now return 404 but i need permanent redirect to new url with readable url
p.s i can't just get new slug string from old links^ so i have to make request for new slug params to server

12 Replies

Avatar
Thrianta
I believe you do it simply using redirect(“/products/work/hammer”) when ur params match “tools” and 12
Avatar
HimalayanOP
how i should redirect user that comes from external resourse just cliking link with old path
and i have more than 1000+ dynamic slugs
Avatar
Thrianta
what do you mean as an external resource?
When a request is sent to your server side route it’ll return a temporary redirect if you use the redirect function
You can have a switch-case detecting those which would make it easier to control those slugs
what’s the reasoning behind the redirection from /tools/12 to /work/hammer
Avatar
HimalayanOP
@Thrianta ru?
Avatar
HimalayanOP
в общем проблема в оптимизации сео страницы так как раньше страница отдавала урлы по конкретному товару через айди но сейчас это изменилось на читаемые тайтлы, соответственно все старые ссылки протухли и теперь никуда не ведут, логично что хотелось бы перенаправлять пользователя на новые актуальные страницы а так как страниц много вручную каждую не прописать соотвественно нужен метод определения старого пути и редиректа на актуальный
Avatar
Thrianta
ну тогда до сих пор можно использовать то что я попытался описать 🥶
you can make some sort of middleware which would match those urls to the new ones
like combine them together with a “_” and then get a redirect value from a hashmap of redirects