Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Release: Opensolr Search 4.5.0 Minor update available for module opensolr_search (4.5.0). Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Usage Milestone: Views Reference Filter Module entityreference_filter crossed 10,000 active installs. Usage Milestone: Dropdown Language Module dropdown_language crossed 10,000 active installs. Usage Milestone: Paragraphs Browser Module paragraphs_browser crossed 10,000 active installs. Usage Milestone: OpenAPI Module openapi crossed 10,000 active installs. Usage Milestone: Decoupled Router Module decoupled_router crossed 10,000 active installs. Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Next.js redirects

7 sites No security coverage Drupal 9–10 · not 11 Next ecosystem
View on drupal.org

This module exposes a REST endpoint that provides a JSON list of redirects. This list can be used to configure redirects in a Next.js application, allowing your Next.js site to dynamically handle redirects managed in Drupal's Redirect module.

If you have a Next.js site that needs to handle redirects based on redirect data managed in a Drupal CMS (via the Redirect module), this module is designed for you.

It follows the "redirects in next.config.js" method documented at:
https://nextjs.org/docs/app/api-reference/config/next-config-js/redirects
https://nextjs.org/docs/app/guides/redirecting#redirects-in-nextconfigjs

🚀 Features

New REST endpoint

Exposes a REST resource at /next_redirects that returns redirect data in JSON format, structured specifically for defining redirects in your next.config.js file. Example:

[
  {
    "source": "/about",
    "destination": "/",
    "permanent": true
  },
  {
    "source": "/articles/redirecting-in-nextjs",
    "destination": "/articles/introducing-nextjs-redirects-module",
    "permanent": true
  }
]

Query string support

Query strings in the source path are correctly parsed and added to the "has" field in the JSON response. Check the Next.js documentation on this topic: https://nextjs.org/docs/app/api-reference/config/next-config-js/redirect...

{
  "source": "/sports/tournaments",
  "destination": "/search",
  "permanent": true,
  "has": [
    {
      "type": "query",
      "key": "sport",
      "value": "soccer"
    },
    {
      "type": "query",
      "key": "team",
      "value": "once-caldas"
    }
  ]
}

Installation

Install the module as any other contributed module for Drupal (https://www.drupal.org/docs/extending-drupal/installing-modules).

Post-Installation

  1. Enable the Next.js Redirects resource at: /admin/config/services/rest
  2. Configure it as follows:
  3. - Granularity: Resource
    - Methods: GET
    - Accepted request formats: JSON
    - Authentication providers: Select whichever you need for this resource

  4. Visit /next_redirects to verify that the endpoint returns the expected output.

Integrating With Next.js

While the endpoint already provides a ready-to-use JSON array, the response still needs to be added to your next.config.js file.
To do this dynamically, we recommend implementing the following approach in your Next.js application:

  1. Create a pre-build script that fetches the redirect data from the Drupal endpoint and saves the result as a JSON file.

    For general guidance on running scripts before every build in Next.js, refer to:
    👉 How to run scripts before every build on Next.js

    In the example below, the contents returned by the Drupal endpoint are saved to /public/static/redirects.json.

    You can copy the following code and place it in a file inside your pre-build directory, for example:
    src/scripts/pre-build/saveRedirects.ts:

    import axios from "axios";
    import fs from "fs";
    import path from "path";
    
    /**
     * Save redirects data retrieved from Drupal to a static file.
     */
    export default async function saveRedirects() {
      try {
        // Get access token using the client credentials grant type.
        // Implement your own getAccessToken function to retrieve the token.
        const drupalAccessToken = await getAccessToken({
          grant_type: "client_credentials",
          client_id: process.env.NEXT_PUBLIC_DRUPAL_CLIENT_ID,
          client_secret: process.env.NEXT_PUBLIC_DRUPAL_CLIENT_SECRET,
        });
    
        // Fetch redirect data from Drupal, this request needs to be authenticated.
        const redirectDataResponse = await axios.get(
          `${process.env.NEXT_PUBLIC_DRUPAL_BASE_URL}/next_redirects`,
          {
          headers: {
            Authorization: `Bearer ${drupalAccessToken}`,
            "Content-Type": "application/json",
          },
          }
        );
        // Generate static file with redirects.
        const destinationFolder = path.join(process.cwd(), "public", "static");
        const fullFilePath = path.join(destinationFolder, "redirects-example.json");
      
        // Create folder for generated data if it doesn't exist yet
        if (!fs.existsSync(destinationFolder)) {
          fs.mkdirSync(destinationFolder);
        }
      
        if (fs.existsSync(fullFilePath)) {
          await fs.promises.unlink(fullFilePath);
        }
      
        await fs.promises.writeFile(fullFilePath, JSON.stringify(redirectDataResponse.data));
    
    
        console.log("Redirects data saved successfully.");
      } catch (error) {
        console.error("Error", error);
      }
    }
    
  2. From your next.config.js, load the redirects from the static JSON file generated in the previous step and return them within the async redirects() function.

    Here's an example of how your redirects() function might look when reading data from public/static/redirects.json:

    import fs from "fs";
    /** @type {import('next').NextConfig} */
    const nextConfig = {
      async redirects() {
        try {
          const redirectsJsonData = await fs.promises.readFile("public/static/redirects.json", "utf-8");
          const redirectsJsonObject = JSON.parse(redirectsJsonData);
          console.log("Successfully read redirects.json file");
          return redirectsJsonObject;
        } catch (error) {
          console.error("Error reading JSON file:", error);
        }
      },
    };
    
  3. Rebuild your Next.js application.

Depends on

Dependencies of the latest stable release

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
3
Tracked since
Oct 2025
Latest release
8 months ago
Releases (12 mo)
3 ▲ from 0
Maintenance
Slowing

Release Timeline

Releases

Version Type Core Release date
1.0.1 Stable 9–10 Dec 28, 2025
1.0.0 Stable 9–10 Oct 17, 2025
1.0.x-dev Dev 9–10 Oct 17, 2025