payloadpack

redirects

by payloadpack
plugins
Redirects & URLs
v4
^4.0.0-beta.12

Overview

Summary generated from the README

Adds a Redirects collection to Payload 4 where editors define exact-path or regex redirects to a URL or a document, and provides a findRedirect function that a page route calls to send visitors on. Lookups use database indexes, so cost stays constant as the list grows. It works with MongoDB, Postgres and SQLite and requires Payload 4.0.0-beta.12 or later.

Features

  • Exact-path and RE2 regex redirects with capture groups filled into the destination as $1 to $9
  • Document targets that follow the page when its path changes
  • Case-insensitive matching for URLs up to 32 levels deep
  • Query-string matching in any order, with tracking parameters such as utm_source kept on the destination
  • Move suggestions created via suggestRedirects, which an editor turns on with one click
  • Permanent (308) or temporary (307) status, an Off switch, and cleanup or refusal of invalid entries on save

Use cases

  • Redirect old URLs to new pages after a site migration or restructure without deleting retired rules
  • Send a whole section such as a blog path to a new section while keeping the rest of the URL
  • Handle large redirect lists on a site with many pages without slowing down lookups

From the author

Redirects editors manage in Payload 4, found through database indexes at the same cost for any number of them. Every page move a site reports, from its own hook or through @payloadpack/path, suggests a redirect from its old path, which an editor turns on with a yes.

Installation

npm i @payloadpack/redirects

10

Stars

0

Forks

0

Open Issues

1 weeks ago

Last Updated

187

Weekly Downloads

v0.1.2

npm Version

260.6 KB

Unpacked Size

1

Dependencies

Repository stars

10+10 since Oct 18

Show data
Repository stars by day (UTC)
DateStars
Dec 6, 202610
Dec 5, 202610
Dec 4, 202610
Dec 3, 202610
Dec 2, 202610
Dec 1, 202610
Nov 30, 202610
Nov 29, 202610
Nov 28, 20269
Nov 27, 20269
Nov 26, 20269
Nov 25, 20269
Nov 24, 20269
Nov 23, 20269
Nov 22, 20269
Nov 21, 20269
Nov 20, 20269
Nov 19, 20269
Nov 18, 202610
Nov 17, 20269
Nov 16, 20268
Nov 15, 20268
Nov 14, 20268
Nov 13, 20268
Nov 12, 20266
Nov 11, 20265
Nov 10, 20263
Nov 9, 20263
Nov 8, 20261
Nov 7, 20261
Nov 6, 20261
Nov 5, 20261
Nov 4, 20261
Nov 3, 20261
Nov 2, 20261
Nov 1, 20261
Oct 31, 20261
Oct 30, 20261
Oct 29, 20261
Oct 28, 20261
Oct 27, 20260
Oct 26, 20260
Oct 25, 20260
Oct 24, 20260
Oct 23, 20260
Oct 22, 20260
Oct 21, 20260
Oct 20, 20260
Oct 19, 20260
Oct 18, 20260

Shared by 3 plugins in payloadpack/plugins.

npm weekly downloads

196+196 since Nov 23

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026196
Dec 5, 2026269
Dec 4, 2026581
Dec 3, 2026592
Dec 2, 2026620
Dec 1, 2026978
Nov 30, 2026929
Nov 29, 2026896
Nov 28, 2026821
Nov 27, 2026495
Nov 26, 2026476
Nov 25, 2026431
Nov 24, 20260
Nov 23, 20260

Each point is npm's rolling 7-day download count.

Health Score

72/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1428386015-redirects.svg)](https://payloaddirectory.dev/plugins/1428386015-redirects)
HTML
<a href="https://payloaddirectory.dev/plugins/1428386015-redirects"><img src="https://payloaddirectory.dev/badge/1428386015-redirects.svg" alt="Payload Directory"></a>
Updated Nov 26, 2026Created Oct 21, 2026Published Nov 25, 2026

README

@payloadpack/redirects

Redirects your editors manage in Payload 4.
Found through database indexes, so a lookup costs the same at 1,000 redirects as at 100,000.

npm version Build status Payload version PolyForm Perimeter license

Documentation · Changelog · Contributing

Compared with Payload's redirects plugin

Payload's own @payloadcms/plugin-redirects only stores the list of redirects. On its own it sends no visitor anywhere: your site needs code that finds the redirect and sends the visitor on. Payload's example website, its Website Template, ships that code, so it's Payload's side below.

Finding one redirect, median (p95), with each database on the same machine as the app, so neither side waits on a network. The example website's lookup reads every redirect, and a list this long is too big for Next.js's data cache, so every lookup reads all of them from the database. Next.js still caches each rendered page, so the lookup runs whenever a page or post renders, pages that exist included: at build, on a path's first visit, each time Next.js renders it again, such as after any redirect is saved, and on every request in draft mode.

Finding one redirect among 100,000@payloadpack/redirectsPayload's example websiteTimes fasterExample website, list cached
MongoDB0.19 ms (0.3 ms)1.3 s (1.4 s)6,400×33.1 ms (34 ms)
Postgres0.36 ms (0.43 ms)1.3 s (1.3 s)3,400×32.4 ms (33.2 ms)
SQLite0.2 ms (0.26 ms)1.5 s (1.6 s)7,200×32.7 ms (34.2 ms)

With a custom cache handler, which Next.js doesn't hold to 2 MB, the example website's lookup parses the cached list instead and costs at least the last column. At 1,000 redirects, where Next.js's data cache holds the list, it makes no database query: close to ours on the same machine, and faster than ours once the database is a round trip away. With the database elsewhere, ours and the uncached read each wait at least one round trip more, and the uncached read pulls the whole list across it. The plugin indexes from, so a lookup you write yourself can find a redirect in one query where from matches exactly as stored, without regexes, and with a query string only in the order and case it was stored.

@payloadpack/redirectsPayload's plugin with Payload's example website
Sends visitors to their redirect✅ The lookup is built in. Your page route calls findRedirect and sends the visitor on, about ten lines.❌ Not on its own: the plugin only stores the list. Your site needs code that finds the redirect and sends the visitor. Payload's example website ships about 90 lines of it, in four files.
Ignores capital letters
/About-Us finds the redirect from /about-us
✅ Any spelling finds it.❌ Only the exact spelling.
Redirects from URLs at any depth
/blog/2019/05/opening-hours
✅ Any URL your site gets, up to 32 levels deep.❌ Only /slug and /posts/slug. No route looks up a redirect for a deeper URL.
Redirects a whole section and keeps the rest of the URL
/blog/(.*) → /news/$1
✅ One regex redirect. Its groups fill into the new URL as $1 to $9.❌ No regexes, and /blog/… is deeper than /slug, so editors can't redirect it.
Tells old URLs apart by what follows the ?
/news.aspx?id=123 and ?id=124 to different pages
✅ Yes, with the query-string proxy, and with the parameters in any order and case.❌ No. Both go wherever /news.aspx goes.
Keeps campaign tags on the way
?utm_source=newsletter
✅ Kept on the new URL, with the query-string proxy.❌ Dropped.
Suggests a redirect when a page moves✅ Your site reports the move, and an editor turns the suggestion on with one click.❌ Nothing. Someone has to notice the old URL and add a redirect by hand.
Redirects to pages at any depth
/about/team/people
✅ Any page's path, nested or not, in every collection you list, and it follows the page when it moves.❌ A picked page goes to /slug and a post to /posts/slug. Any other path must be typed as a URL, which doesn't follow the page when it moves.
Permanent or temporary, as editors pick✅ Visitors get what the editor picked: permanent (308) or temporary (307).❌ The plugin can store one of five status codes editors pick. Payload's example website doesn't turn it on and always sends temporary.
Turns a redirect off without deleting it✅ Set it to Off.❌ Delete it.
Catches mistakes when you save
a mailto: link to redirect from
✅ Cleans each one and refuses what can't work.❌ Only checks that the fields are filled in and no old URL is used twice.

Speed shows 1,000 and 10,000 redirects, the exact code each side runs, and how to run the benchmark yourself from this repository.

What you get

  • ↪️ Exact paths and regexes: editors send visitors from an exact path or a regex to a document on your site or a URL
  • 📄 Document targets follow moves: a redirect to a document sends visitors wherever the document is now, even after later moves
  • 🧭 Move suggestions: report page moves from your own hook or @payloadpack/path to create one suggestion per moved branch, which an editor answers right on the moved page
  • 🔎 Old URLs with a query string: matches /news.aspx?id=123 in any order and case, without comparing tracking parameters such as utm_source, which every destination keeps
  • ⚡ Constant lookup cost: one indexed query reads only the redirects and regexes that can cover a path, so 100,000 cost the same as 1,000, plus one query when the winner targets a document
  • 🛡️ RE2 regexes and host-locked destinations: regexes never backtrack, and destinations never leave their host whatever a visitor's path fills in

Install

bun add @payloadpack/redirects

Add redirectsPlugin to payload.config.ts with the collections a redirect can send visitors to, buildDocumentPath, and documentSelect:

import { redirectsPlugin } from '@payloadpack/redirects'
import { buildConfig } from 'payload'

export default buildConfig({
  plugins: [
    redirectsPlugin({
      collections: ['pages'],
      buildDocumentPath: ({ document }) => `/${document.slug}`,
      documentSelect: { slug: true }
    })
  ]
})

With @payloadpack/path, return the path it stores: buildDocumentPath: ({ document }) => document.path with documentSelect: { path: true }.

Look up a redirect in your page route when no document holds the path:

import { findRedirect } from '@payloadpack/redirects'

const { foundRedirect } = await findRedirect({ path: '/old-page', payload })
// { destination: '/new-page', matchedQueryString: false, permanent: true, to: { type: 'document' } } or null

permanent is true for 308 and false for 307. Pass hasDocument to follow chains in one hop, and give visitors read access to published documents in the collections listed in collections.

See the Installation guide, Match query strings in a proxy, and Let redirects win over pages.

Suggest redirects on page moves

To suggest a redirect when a page moves, call suggestRedirects with each document's old and new path. This example covers a collection without drafts:

import type { CollectionAfterChangeHook, CollectionConfig } from 'payload'
import { suggestRedirects } from '@payloadpack/redirects'

// Your site's own way of building a page's path.
const buildPath = (page: { slug: string }) => `/${page.slug}`

const suggestRedirectOnMove: CollectionAfterChangeHook = async ({ doc, operation, previousDoc, req }) => {
  if (operation !== 'update') return doc
  await suggestRedirects({
    changedPaths: [{ collection: 'pages', id: doc.id, from: buildPath(previousDoc), to: buildPath(doc) }],
    payload: req.payload,
    req
  })
  return doc
}

export const Pages: CollectionConfig = {
  slug: 'pages',
  hooks: { afterChange: [suggestRedirectOnMove] },
  fields: [{ name: 'slug', type: 'text', required: true }]
}

With @payloadpack/path, pass onPathsChanged: suggestRedirects or call await suggestRedirects(args) inside your existing onPathsChanged. See Suggest redirects on a move.

Configure

OptionPurposeDefault
collectionsCollections a redirect can send visitors to a document ofRequired
buildDocumentPathBuilds the path of a document a redirect sends visitors to from the documentRequired
documentSelectFields buildDocumentPath reads, so a lookup reads only thoseWhole document
trackingParametersExtra query string parameters no redirect compares, beside utm_source and the other defaultsBuilt-in defaults
winOverDocumentsLets redirects win over a page at their path: selected redirects ('perRedirect') or every redirect that is On ('always')Omitted (page wins)
overridesFunction that takes the Redirects collection and returns it changedNone

See Configuration, findRedirect, and Refusals and errors.

Speed

Without a proxy, your page route asks for a redirect only when no page holds the path, so normal page views pay nothing. With the query-string proxy, this is what it adds to each page view at 100,000 redirects, median (p95). See Speed for how it is measured and how to run it yourself.

Without a query stringWith one, database on the same machineWith one, database 5 ms away
MongoDB0.06 ms (0.09 ms)0.62 ms (0.86 ms)5.81 ms (6.25 ms)
Postgres0.08 ms (0.13 ms)0.55 ms (0.77 ms)5.78 ms (6.02 ms)
SQLite0.08 ms (0.1 ms)0.34 ms (0.53 ms)Same machine only

With winOverDocuments, the proxy checks every page view instead and adds:

Database on the same machineDatabase 5 ms away
MongoDB0.47 ms (0.81 ms)5.77 ms (6.25 ms)
Postgres0.55 ms (0.76 ms)5.88 ms (6.81 ms)
SQLite0.36 ms (0.59 ms)Same machine only

Requirements

Payload 4.0.0-beta.12 or later, with MongoDB, Postgres, or SQLite. @payloadpack/path 0.3.1 or later, if you use it.

License

Use it in any project, including client work you charge for and products you sell. What you can't do is offer the plugin, or a copy or fork of it, to others as a competing plugin, paid or free. The terms are the PolyForm Perimeter License 1.0.1.

Similar plugins