New guide: AI is in production. Is your governance?

Announcing Visual Copilot - Figma to production in half the time

Builder.io
Builder.io
Contact sales

New guide: AI is in production. Is your governance?

Announcing Visual Copilot - Figma to production in half the time

This tutorial explains how to set up server-side redirects using Builder's Data model.

Server-side redirects guide visitors from one URL to another before the page loads. Use redirects to phase out old URLs without breaking links or to transition to new naming conventions.

  • Redirects run on the server, so the intermediate URL does not display.
  • Each redirect maps a source URL to a destination URL and can optionally mark the redirect as permanent with a 301 HTTP status.
  • Manage redirects in Builder using a single Data model without code changes until deployment.
  • An app in the framework of your choice with the appropriate Builder SDK installed.
  • Integrate your app with a Data model. For details, see Integrate with structured Data.

Create a Data Model to define a structured container for your site's URL redirects.

To create a Data model:

  1. Go to Models.
  2. Click + Create Model.
  3. In the Name field, enter URL Redirects.
  4. In the Description field, enter server-side redirects.
  5. Click Create.

The video below demonstrates creating a new Data model called URL Redirects:

Add custom fields to the model to store the redirect URLs and other settings for managing redirects in your content entries.

To add custom fields:

  1. On the Fields tab, click + New Field.
  2. In the Name field, enter SourceUrl and set the Type to Url.
  3. Repeat the process to create DestinationUrl with type Url and RedirectToPermanent with type Boolean.
  4. Click Save.

The video below demonstrates adding custom fields such as SourceUrl, DestinationUrl, and RedirectToPermanent to the Data model.

To create URL Redirects entries:

  1. Go to Content and click + New Entry.
  2. Select URL Redirect as the model type.
  3. In the Name field, enter /site/intro → /site/get-started to make the entry easy to identify.
  4. Assign values to each custom field: SourceUrl, DestinationUrl, and RedirectToPermanent.
  5. Click Save.

The following video demonstrates creating a content entry using the URL Redirects model and setting values for the custom fields.

Create or update the next.config.js file in the root directory:

// next.config.js

const { builder } = require("@builder.io/sdk");

module.exports = {
  async redirects() {
    const results = await builder.getAll("url-redirects");

    return results.map(({ data }) => ({
      source: data.sourceUrl,
      destination: data.destinationUrl,
      permanent: !!data.redirectToPermanent,
    }));
  },
};

This setup uses builder.getAll() to fetch all entries from the url-redirects model. Each entry is transformed into the format required by Next.js:

  • source: the original URL from sourceUrl
  • destination: the redirect target from destinationUrl
  • permanent: true for permanent HTTP status code 301, false for temporary HTTP status code 302

For example, a redirect created in Builder from /old-page to /new-page with permanent set to true maps to:

{
  source: "/old-page",
  destination: "/new-page",
  permanent: true
}

The next.config.js file supports up to 1024 redirect entries. For more context, refer to Vercel's guide, How can I increase the limit of redirects or use dynamic redirects on Vercel? and for more comprehensive information, visit their Redirecting documentation.

With Next.js sites, you need to trigger a rebuild for new redirects. Since Next.js evaluates the next.config.js only once when building an app, newly added redirects don’t take effect immediately. To cause this rebuild, use a webhook.

For this part of the tutorial, you need a webhook URL on the backend. If you're using Vercel, visit their Deploy hooks documentation.

To add a Webhook to the data model take the following steps:

  1. Go to Models.
  2. Open the URL Redirects model you made in Create a Data model.
  3. On the Advanced tab, go to Webhooks and click Edit.
  4. Click + Webhook.
  5. Expand the new Webhook 1 and paste your webhook URL into the URL field.
  6. Click Done.

The following video demonstrates how to set up a Webhook:

In entry.server.tsx, import findRedirectByRequestUrl() and add the redirect logic at the top of the handleRequest() function.

Was this article helpful?