typescriptbeginner

Redirect Matrix in next.config

Define URL redirect rules in next.config.ts for SEO migration, vanity URLs, and path normalization.

typescript
// next.config.ts
import type { NextConfig } from 'next';

const redirects: { source: string; destination: string; permanent: boolean }[] = [
  // Old blog paths -> new
  { source: '/blog/:slug', destination: '/posts/:slug', permanent: true },
  // Vanity URL
  { source: '/pricing', destination: '/plans', permanent: false },
  // Remove trailing slash
  { source: '/:path+/', destination: '/:path+', permanent: true },
  // External redirect
  { source: '/docs', destination: 'https://docs.example.com', permanent: false },
];

const config: NextConfig = {
  async redirects() {
    return redirects;
  },
};

export default config;

Use Cases

  • URL migration after redesign
  • Vanity URLs
  • Trailing slash normalization

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.