Middleware not working in Next.js

My middleware.ts file is in the root of the project, and I have this code in it:

import { NextRequest, NextResponse } from "next/server";

export function middleware(request: NextRequest) {
  return NextResponse.redirect(new URL("/new-page", request.url));

I followed the Next.js course, what am I doing wrong?

It doesn’t redirect me to /new-page.

Hey have you figured it out? Running into same issue…

Ok i think I figured it out. I moved middleware.ts inside /src, but not inside /src/app. It is at the same level as /src/app. I’m using Next 14.1 btw.

For more info: Routing: Middleware | Next.js

1 Like