#Redirecting to a dynamic URL

14 messages · Page 1 of 1 (latest)

wicked sleet
#

I have a URL that is /college-football/rankings/[division]/[year]/[week]. I want to be able to redirect a user who goes to lets say /college-football/rankings/fcs/2024 to the latest week that I have rankings for. However, not entirely sure where to put that redirect.

urban tulipBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

twin flame
deep wadi
wicked sleet
#

@deep wadi I don’t think that would work I would have to create a page.tsx in the [year] directory in order to redirect. That’s what Next would be trying to render and would result in a 404

deep wadi
deep wadi
#

@wicked sleettried?

meager island
# deep wadi you can still do something like this: ```tsx import {redirect, RedirectType} fro...

actually I have something similar in my codebase and the redirect works amazingly well when you land on the page, but if you do an hard reload on a sibyl folder you get redirected to the redirect path.

In my case I have this structure:

- project/
    - [projectId]
        - analytics/
        - sources/
        - findings/
    page.tsx

Let's say I land on /project/123 I get redirect to project/123/analytics (assuming that analytics is the URL I will redirect to), the issue pops up if I hard refresh project/123/sources/ I do get redirected to project/123/analytics 😞

There is an approach that will allow me to ignore this file if I am looking for a sub-folder?

deep wadi
meager island
#

you are correct, that's the strange part for me too.

If I land on the page while inside the same session of Next.js I can load the page withhout any issues.

But if I type in my browser project/123/sources/ and press enter, I get redirected to project/123/analytics/

And I am pretty sure that the [projectId] get's loaded because I placed a console.log there 😞

deep wadi
#

And I am pretty sure that the [projectId] get's loaded because I placed a console.log there 😞
never ever.

Can you provide a repro repo via https://codesandbox.io/ for exmaple?

meager island
#

Never mind, my friend, I am really thankful for your support, but luckily, I figured out where the problem was.

While I was able to read the console.log placed in project/[projectId]/page.tsx the issue wasn't from it, but from different select that I had to build.

Basically speaking, I have a bunch of "filters" that allow the visitor to select different values to organize the data he's seeing. Each select is controlling a slug of the path.

The control I had in place didn't account of the last folder (the page) I was rendering and the router.push I set brought me back to the default page ( project/[projectId]/page.tsx) and from that I was getting redirect to /analytics because that page had a redirect pointing to it.

Seems that now I've been able to solve it, this means that also [the answer you gave to the OP](#1264717388356653099 message) results correct 💪

deep wadi
#

@wicked sleet is your issue resolved like that?

deep wadi
#

@wicked sleet ?