Next.js Discord

Discord Forum

Change the path for routing

Answered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
Hey ! I just wanna know if we can change the default path for routing in the app router like src/app/Presentation/Pages/** ?
Answered by B33fb0n3
yes. You can also rewrite the specific request. So the user visit / but in the background the user is on /Presentation/Pages/. The rewrite could be done by a middleware
View full answer

9 Replies

@Masai Lion Hey ! I just wanna know if we can change the default path for routing in the app router like src/app/Presentation/Pages/** ?
you can create a folder structure like src/app/Presentation/Pages/ and create all the needed files in these folder. Like that you directly get the path and can build and work with it
@Masai Lion yes, but my path will be /Presentation/Pages for the index page ?
yes. You can also rewrite the specific request. So the user visit / but in the background the user is on /Presentation/Pages/. The rewrite could be done by a middleware
Answer
@Masai Lion Ok, i will check that thanks !
Feel free to respond here again if you have issues with the provided solution 👍
Masai LionOP
Yep, i wrote a new middleware.ts at base of my project to rewrite the URL :
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

export function middleware(request: NextRequest) {
  const url = request.nextUrl.clone()

  const prefix = '/Presentation/Pages'
  url.pathname = `${prefix}${url.pathname}`

  return NextResponse.rewrite(url)
}