Next.js middleware throws 'URL is malformed. Please use only absolute URLs'
Error: URL is malformed "/login". Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urlsShort answer
Next.js refuses any redirect or rewrite in middleware (proxy.ts in Next.js 16) whose target is not a full URL. The message prints the rejected value, so read it first. Usually it is a bare path such as /login, or a base built from an environment variable that is empty or has no https://. Build the URL from the request instead.
Fix it fast
1Find out which cause you have
Read the value inside the quotes of the error message. It is the exact string that was passed to redirect or rewrite, and its shape tells you which cause below applies.
grep -rnE "(redirect|rewrite)\(" middleware.ts proxy.ts src/middleware.ts src/proxy.ts 2>/dev/null2Match what you see, then jump to its fix
- 1
The quoted value starts with a slash, for example "/login" or "/dashboard"A path without a host is passed to redirect or rewriteFix - 2
The quoted value starts with the word undefined, for example "undefined/login"The base URL comes from an environment variable that is not setFix - 3
The quoted value starts with a bare host name, for example "example.com/login"The configured base has no https:// in front of itFix
- 1
What does the error look like?
Error: URL is malformed "/login". Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urlsURL is malformed "undefined/login"
Cause 1 of 3
A path without a host is passed to redirect or rewrite
1Check
NextResponse.redirect(), NextResponse.rewrite() and Response.redirect() all need a complete URL. A bare path cannot be turned into a request, so Next.js stopped accepting it after version 12.1.
grep -rnE "redirect\(['\"\`]/|rewrite\(['\"\`]/" middleware.ts proxy.ts src 2>/dev/null2Fix
Clone the incoming URL and change only the part you need. This keeps basePath and locale. Building the URL with new URL(path, request.url) also works, but it ignores both.
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const url = request.nextUrl.clone()
url.pathname = '/login'
return NextResponse.redirect(url)
}3Verify
Request a protected page without a session; the response is a redirect to the login page and the error is gone from the server log.
curl -I http://localhost:3000/dashboardConfirmed by nextjs.org
Cause 2 of 3
The base URL comes from an environment variable that is not set
1Check
A template string such as `${process.env.APP_URL}/login` becomes undefined/login when the variable is missing.
grep -rn "APP_URL" .env .env.local .env.production 2>/dev/null
grep -rn "process.env" middleware.ts proxy.ts src 2>/dev/null2Fix
Stop depending on a variable for the host at all: take the origin from the request that arrived. If you really need a configured value, set it in every environment and restart.
const url = new URL('/login', request.url)
return NextResponse.redirect(url)3Verify
Restart the dev server and hit a protected route; the redirect now carries a full http://localhost:3000/login address.
curl -sI http://localhost:3000/dashboard | grep -i '^location'Confirmed by raw.githubusercontent.com
Cause 3 of 3
The configured base has no https:// in front of it
1Check
A URL is absolute only when it carries a scheme. example.com/login is a host plus a path with no scheme, so the URL parser throws.
node -e "try { new URL('example.com/login') } catch (e) { console.log(e.name + ': ' + e.message) }"2Fix
Write the scheme into the variable (https://example.com), or build the redirect from the request origin so no variable is needed.
# .env.production
APP_URL=https://example.com
// middleware.ts
return NextResponse.redirect(new URL('/login', process.env.APP_URL))3Verify
Run the same node line with the corrected value; it prints the full address instead of throwing.
node -e "console.log(new URL('/login', 'https://example.com').href)"Confirmed by developer.mozilla.org
How do I stop it coming back?
- Write redirects in one helper that always starts from request.nextUrl.clone(), so no one types a bare path again.
- Keep host names out of environment variables where the incoming request already knows the origin.
- If a variable must hold a site address, store it with the scheme and add a startup check that new URL() accepts it.
- After moving to Next.js 16, rename middleware.ts to proxy.ts and the exported function to proxy; the rule about absolute URLs stays the same.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Upgrading to Next.js 16: middleware becomes proxy, and the other things that break
Missing Required default.js for Parallel Route
Next.js 16 renames middleware.ts to proxy.ts and its exported function to proxy.
6 causes, each with a fixBack end
TypeError: Failed to parse URL when a Next.js server fetches its own API
TypeError: Failed to parse URL from undefined/api/test
Server code has no page address, so fetch('/api/test') cannot be turned into a full URL and throws Failed to parse URL.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fix