Skip to content

@payloadpack/redirectsGuides

Let redirects win over pages

Configure winOverDocuments and a Next.js proxy so selected or all redirects answer even where a page holds the path.

You want a redirect to answer at a path where a page is still published, without unpublishing the page.

With this setup, every page view pays one database query; see Speed. Skip this guide if your redirects only cover paths no page holds.

Prerequisites: Complete Installation on a Next.js 16 site using proxy.ts.

By default, a page wins over every redirect at its path except one that matched the visitor’s query string.

  1. Set winOverDocuments in payload.config.ts.

    payload.config.ts
    redirectsPlugin({
    collections: ['pages'],
    buildDocumentPath: ({ document }) => `/${document.slug}`,
    documentSelect: { slug: true },
    winOverDocuments: 'perRedirect'
    })
    • 'perRedirect' adds a Wins over a page (winsOverDocument) box to each redirect, unchecked by default and on every suggestion an editor says yes to. Editors check the box on redirects that should win over a page.
    • 'always' lets every redirect that is On win over a page and adds no box.

    A redirect that matched the visitor’s query string wins over a page under both settings. Setting 'perRedirect' adds a winsOverDocument column, so run bunx payload migrate:create on Postgres and SQLite.

  2. Answer every redirect in proxy.ts.

    Because a redirect can now cover a path where a page exists, the proxy checks every request, with or without a query string. The same proxy works for both 'perRedirect' and 'always'.

    Put proxy.ts next to the src/find-page.ts you wrote in step 5 of Installation, whose findPage it calls.

    src/proxy.ts
    import payloadConfig from '@payload-config'
    import { findRedirect } from '@payloadpack/redirects'
    import { NextResponse, type NextRequest } from 'next/server'
    import { getPayload } from 'payload'
    import { findPage } from './find-page'
    // Answers every redirect, since one can win over the page at its path. findRedirect decides by
    // winOverDocuments whether a page wins, and asks hasDocument only when it needs to.
    export async function proxy(request: NextRequest) {
    let path: string
    try {
    path = decodeURIComponent(request.nextUrl.pathname)
    } catch {
    return NextResponse.next()
    }
    const payload = await getPayload({ config: payloadConfig })
    const hasDocument = async (pathAsked: string) => (await findPage({ path: pathAsked, payload })) !== null
    const { foundRedirect } = await findRedirect({ hasDocument, path, payload, searchParams: request.nextUrl.searchParams })
    if (!foundRedirect) return NextResponse.next()
    // A path on this site goes below its basePath, which Next adds to a page route's redirect() itself.
    const { destination, permanent } = foundRedirect
    const url = destination.startsWith('/') ? `${request.nextUrl.basePath}${destination}` : destination
    return NextResponse.redirect(new URL(url, request.url), permanent ? 308 : 307)
    }
    // Skips the admin, the API and Next's own files, whole segments only, so a path such as /apiary passes.
    export const config = { matcher: ['/((?!(?:admin|api|_next)(?:/|$)).*)'] }
  3. Remove the redirect check from your page route.

    The proxy now answers every redirect before the page route runs, so your page route only renders the page or calls notFound().

  • Every page view pays a lookup: Each request runs one database query. A redirect to a document costs two more queries: one to read the document’s path and one lookup at that path, because another redirect can win over that page too. Each step of a chain costs one lookup. See Speed.

  • Files in public/: Requests for static files such as /favicon.ico pass the default matcher and run a lookup. Exclude static files your site serves in config.matcher.

  • hasDocument with 'perRedirect': findRedirect asks hasDocument only when the best candidate cannot win over a page by itself. With 'always', findRedirect never calls hasDocument and follows chains without it. Calling findRedirect without hasDocument when winOverDocuments is 'perRedirect' throws:

    @payloadpack/redirects: give findRedirect hasDocument, which says whether a document holds a path, because with winOverDocuments ‘perRedirect’ a document at the path wins over every redirect but the ones an editor picked and one that matched the query string

  • CDN caching: A CDN in front of the proxy serves pages it cached before the proxy runs, so a redirect saved later reaches visitors only after that path is purged at the CDN. Next’s CDN caching guide recommends running the proxy before the CDN cache or skipping CDN caching for routes that depend on it.