Adds a redirects collection to Payload so editors can manage URL redirects in the admin panel. Each redirect points to either a document from configured collections or a custom URL, with optional redirect types such as 301 and 302. The collection is queried from the frontend, for example in Next.js middleware, to apply the redirects.
Redirects plugin for Payload
Installation
npm i @payloadcms/plugin-redirects45.4k
Stars
4.3k
Forks
1380
Open Issues
Today
Last Updated
105.3k
Weekly Downloads
v3.91.0
npm Version
36.8 KB
Unpacked Size
2
Dependencies
Trends appear after 7 days of data. Daily snapshots have not started yet.
Shared by 12 plugins in payloadcms/payload.
Trends appear after 7 days of data. Daily snapshots have not started yet.
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/official-plugin-redirects)<a href="https://payloaddirectory.dev/plugins/official-plugin-redirects"><img src="https://payloaddirectory.dev/badge/official-plugin-redirects.svg" alt="Payload Directory"></a>README
A plugin for Payload to easily manage your redirects from within your admin panel.
pnpm add @payloadcms/plugin-redirects
In your Payload Config, add the plugin:
import { buildConfig } from 'payload'
import { redirectsPlugin } from '@payloadcms/plugin-redirects'
export default buildConfig({
plugins: [
redirectsPlugin({
collections: ['pages'], // Collections to use in the 'to' relationship field
}),
],
})
| Option | Type | Description |
|---|---|---|
collections | string[] | An array of collection slugs to populate in the to field of each redirect. |
overrides | object | A partial collection config that allows you to override anything on the redirects collection. |
redirectTypes | string[] | Provide an array of redirects if you want to provide options for the type of redirects to be supported. |
redirectTypeFieldOverride | Field | A partial Field config that allows you to override the Redirect Type field if enabled above. |
import { buildConfig } from 'payload'
import { redirectsPlugin } from '@payloadcms/plugin-redirects'
export default buildConfig({
plugins: [
redirectsPlugin({
collections: ['pages', 'posts'],
// Add custom redirect types
redirectTypes: ['301', '302'],
// Override the redirects collection
overrides: {
slug: 'custom-redirects',
// Add custom fields
fields: ({ defaultFields }) => {
return [
...defaultFields,
{
name: 'notes',
type: 'textarea',
admin: {
description: 'Internal notes about this redirect',
},
},
]
},
},
}),
],
})
The plugin automatically includes translations for English, French, and Spanish. If you want to customize existing translations or add new languages, you can override them in your config:
import { buildConfig } from 'payload'
import { redirectsPlugin } from '@payloadcms/plugin-redirects'
export default buildConfig({
i18n: {
translations: {
// Add your custom language
de: {
'plugin-redirects': {
fromUrl: 'Von URL',
toUrlType: 'Ziel-URL-Typ',
internalLink: 'Interner Link',
customUrl: 'Benutzerdefinierte URL',
documentToRedirect: 'Dokument zum Weiterleiten',
redirectType: 'Weiterleitungstyp',
},
},
// Or override existing translations
fr: {
'plugin-redirects': {
fromUrl: 'URL source', // Custom override
},
},
},
},
plugins: [
redirectsPlugin({
collections: ['pages'],
}),
],
})
The plugin creates a redirects collection in your database. You can query this collection from your frontend and implement the redirects using your framework's routing system.
// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Fetch redirects from Payload API
const redirects = await fetch('http://localhost:3000/api/redirects', {
next: { revalidate: 60 }, // Cache for 60 seconds
}).then((res) => res.json())
// Find matching redirect
const redirect = redirects.docs?.find((r: any) => r.from === pathname)
if (redirect) {
const destination =
redirect.to.type === 'reference'
? redirect.to.reference.slug // Adjust based on your collection structure
: redirect.to.url
return NextResponse.redirect(
new URL(destination, request.url),
redirect.type === '301' ? 301 : 302,
)
}
return NextResponse.next()
}
Topics
redirects
Managed redirects collection with a cache-backed Next.js middleware for fast edge-safe matching
560/wk15v3
payload slug redirects
Records slug changes and supplies Next.js with data to redirect old URLs to the current document
340/wk3v3
nested docs
Nest documents inside one another in Payload collections to build hierarchical page structures
10090.8k/wk45.4kv3
localized slugs
Copies localized slugs and full paths into a single localizedSlugs field for multi-locale Payload collections
650/wk9v3
payload page tree
Folder-based hierarchical URL slugs with a visual tree view for managing pages in the Payload admin
590/wk19v3
paths
Stored, queryable document paths for Payload page trees with publish-time uniqueness and a framework-agnostic resolver
560/wk15v3