Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Next.js middleware throws 'URL is malformed. Please use only absolute URLs'

ErrorError: URL is malformed "/login". Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls

Short 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.

  • Applies to Next.js 15, Next.js 16, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/null
  2. 2Match what you see, then jump to its fix

    1. 1The quoted value starts with a slash, for example "/login" or "/dashboard"A path without a host is passed to redirect or rewriteFix
    2. 2The quoted value starts with the word undefined, for example "undefined/login"The base URL comes from an environment variable that is not setFix
    3. 3The quoted value starts with a bare host name, for example "example.com/login"The configured base has no https:// in front of itFix

What does the error look like?

  • Error: URL is malformed "/login". Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls
  • URL 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/null

2Fix

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/dashboard

Confirmed by nextjs.org

Did this fix it?

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/null

2Fix

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

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.