Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
7 replies
utdev

Nextjs Middleware to redirect

Hi I want to redirect this route / to /dashboard for that I created a middleware.ts inside src

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(req: NextRequest) {
  if (req.nextUrl.pathname === '/') {
    return NextResponse.redirect('/dashboard')
  }
}


If I access / now I get this error
Error: URLs is malformed. Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls


What am I missing?
Was this page helpful?