|
2 | 2 |
|
3 | 3 | A plugin for [Payload](https://github.com/payloadcms/payload) to easily manage your redirects from within your admin panel. |
4 | 4 |
|
| 5 | +## Features |
| 6 | + |
| 7 | +- Manage redirects directly from your admin panel |
| 8 | +- Support for internal (reference) and external (custom URL) redirects |
| 9 | +- Built-in multi-language support |
| 10 | +- Optional redirect types (301, 302, etc.) |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +pnpm add @payloadcms/plugin-redirects |
| 16 | +``` |
| 17 | + |
| 18 | +## Basic Usage |
| 19 | + |
| 20 | +In your [Payload Config](https://payloadcms.com/docs/configuration/overview), add the plugin: |
| 21 | + |
| 22 | +```ts |
| 23 | +import { buildConfig } from 'payload' |
| 24 | +import { redirectsPlugin } from '@payloadcms/plugin-redirects' |
| 25 | + |
| 26 | +export default buildConfig({ |
| 27 | + plugins: [ |
| 28 | + redirectsPlugin({ |
| 29 | + collections: ['pages'], // Collections to use in the 'to' relationship field |
| 30 | + }), |
| 31 | + ], |
| 32 | +}) |
| 33 | +``` |
| 34 | + |
| 35 | +## Configuration |
| 36 | + |
| 37 | +### Options |
| 38 | + |
| 39 | +| Option | Type | Description | |
| 40 | +| --------------------------- | ---------- | ------------------------------------------------------------------------------------------------------- | |
| 41 | +| `collections` | `string[]` | An array of collection slugs to populate in the `to` field of each redirect. | |
| 42 | +| `overrides` | `object` | A partial collection config that allows you to override anything on the `redirects` collection. | |
| 43 | +| `redirectTypes` | `string[]` | Provide an array of redirects if you want to provide options for the type of redirects to be supported. | |
| 44 | +| `redirectTypeFieldOverride` | `Field` | A partial Field config that allows you to override the Redirect Type field if enabled above. | |
| 45 | + |
| 46 | +### Advanced Example |
| 47 | + |
| 48 | +```ts |
| 49 | +import { buildConfig } from 'payload' |
| 50 | +import { redirectsPlugin } from '@payloadcms/plugin-redirects' |
| 51 | + |
| 52 | +export default buildConfig({ |
| 53 | + plugins: [ |
| 54 | + redirectsPlugin({ |
| 55 | + collections: ['pages', 'posts'], |
| 56 | + |
| 57 | + // Add custom redirect types |
| 58 | + redirectTypes: ['301', '302'], |
| 59 | + |
| 60 | + // Override the redirects collection |
| 61 | + overrides: { |
| 62 | + slug: 'custom-redirects', |
| 63 | + |
| 64 | + // Add custom fields |
| 65 | + fields: ({ defaultFields }) => { |
| 66 | + return [ |
| 67 | + ...defaultFields, |
| 68 | + { |
| 69 | + name: 'notes', |
| 70 | + type: 'textarea', |
| 71 | + admin: { |
| 72 | + description: 'Internal notes about this redirect', |
| 73 | + }, |
| 74 | + }, |
| 75 | + ] |
| 76 | + }, |
| 77 | + }, |
| 78 | + }), |
| 79 | + ], |
| 80 | +}) |
| 81 | +``` |
| 82 | + |
| 83 | +## Custom Translations |
| 84 | + |
| 85 | +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: |
| 86 | + |
| 87 | +```ts |
| 88 | +import { buildConfig } from 'payload' |
| 89 | +import { redirectsPlugin } from '@payloadcms/plugin-redirects' |
| 90 | + |
| 91 | +export default buildConfig({ |
| 92 | + i18n: { |
| 93 | + translations: { |
| 94 | + // Add your custom language |
| 95 | + de: { |
| 96 | + 'plugin-redirects': { |
| 97 | + fromUrl: 'Von URL', |
| 98 | + toUrlType: 'Ziel-URL-Typ', |
| 99 | + internalLink: 'Interner Link', |
| 100 | + customUrl: 'Benutzerdefinierte URL', |
| 101 | + documentToRedirect: 'Dokument zum Weiterleiten', |
| 102 | + redirectType: 'Weiterleitungstyp', |
| 103 | + }, |
| 104 | + }, |
| 105 | + // Or override existing translations |
| 106 | + fr: { |
| 107 | + 'plugin-redirects': { |
| 108 | + fromUrl: 'URL source', // Custom override |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + |
| 114 | + plugins: [ |
| 115 | + redirectsPlugin({ |
| 116 | + collections: ['pages'], |
| 117 | + }), |
| 118 | + ], |
| 119 | +}) |
| 120 | +``` |
| 121 | + |
| 122 | +## Using Redirects in Your Frontend |
| 123 | + |
| 124 | +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. |
| 125 | + |
| 126 | +### Example: Next.js Middleware |
| 127 | + |
| 128 | +```ts |
| 129 | +// middleware.ts |
| 130 | +import { NextResponse } from 'next/server' |
| 131 | +import type { NextRequest } from 'next/server' |
| 132 | + |
| 133 | +export async function middleware(request: NextRequest) { |
| 134 | + const { pathname } = request.nextUrl |
| 135 | + |
| 136 | + // Fetch redirects from Payload API |
| 137 | + const redirects = await fetch('http://localhost:3000/api/redirects', { |
| 138 | + next: { revalidate: 60 }, // Cache for 60 seconds |
| 139 | + }).then((res) => res.json()) |
| 140 | + |
| 141 | + // Find matching redirect |
| 142 | + const redirect = redirects.docs?.find((r: any) => r.from === pathname) |
| 143 | + |
| 144 | + if (redirect) { |
| 145 | + const destination = |
| 146 | + redirect.to.type === 'reference' |
| 147 | + ? redirect.to.reference.slug // Adjust based on your collection structure |
| 148 | + : redirect.to.url |
| 149 | + |
| 150 | + return NextResponse.redirect( |
| 151 | + new URL(destination, request.url), |
| 152 | + redirect.type === '301' ? 301 : 302, |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + return NextResponse.next() |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## Links |
| 161 | + |
5 | 162 | - [Source code](https://github.com/payloadcms/payload/tree/main/packages/plugin-redirects) |
6 | 163 | - [Documentation](https://payloadcms.com/docs/plugins/redirects) |
7 | | -- [Documentation source](https://github.com/payloadcms/payload/tree/main/docs/plugins/redirects.mdx) |
| 164 | +- [Issue tracker](https://github.com/payloadcms/payload/issues) |
0 commit comments