All articles

Redirecting legacy .aspx URLs with query strings in Next.js 16 and Drupal

In a decoupled Drupal site with a Next.js 16 front end, a Redirect module entry for an old address such as /services/page.aspx?id=42 never fires, because the catch-all page route resolves only the path and never passes the query string to Drupal. The fix is to catch file-like legacy addresses that carry a query in proxy.ts, ask Drupal's translate-path endpoint about the full path plus query, and redirect. Then keep that redirect, and the lookup behind it, out of long-lived caches, or a redirect an editor deletes keeps working for hours.

I hit all three parts of this on a public-sector site whose previous platform left plenty of inbound links pointing at old .aspx pages.

Why the Drupal redirect never reached the front end

The old addresses lived in the Drupal Redirect module, and some of them included a query string, because on an old platform it is common for one script to serve many documents by an id parameter. Drupal's Redirect module can match on the query. The front end never gave it the chance.

The front end resolves every request through a catch-all route. It takes the path segments, asks Drupal (via the Decoupled Router module's /router/translate-path endpoint) what lives at that path, and either renders the entity or follows the redirect that the endpoint returns. The route only ever receives the path segments. The query string is simply not part of what a dynamic segment sees, so Drupal was asked about /services/page.aspx, which has no redirect, and answered with a 404.

Passing the query into every page lookup would have fixed it, but at a cost: every query-string variation of every normal page would become a distinct lookup and a distinct cache entry. Tracking parameters alone would multiply them.

How to look up legacy redirects in proxy.ts

Next.js 16 renamed middleware.ts to proxy.ts. It still runs before routing and still sees the full URL, query included. So the lookup can live there, limited to the only addresses that need it: old file-like paths that carry a query.

import { NextResponse, type NextRequest } from "next/server";

const LEGACY_FILE = /\.(aspx?|php|html?|cfm|jsp)$/i;

async function legacyRedirect(req: NextRequest) {
  const { pathname, search } = req.nextUrl;
  if (!search || !LEGACY_FILE.test(pathname)) return null;

  const lookup = new URL("/router/translate-path", process.env.DRUPAL_URL);
  lookup.searchParams.set("_format", "json");
  lookup.searchParams.set("path", decodeURIComponent(pathname) + search);
  lookup.searchParams.set("cb", String(Date.now()));

  try {
    const res = await fetch(lookup, {
      cache: "no-store",
      signal: AbortSignal.timeout(3000),
    });
    if (!res.ok) return null;
    const target = (await res.json())?.redirect?.[0];
    if (!target?.to) return null;

    const redirect = NextResponse.redirect(
      new URL(target.to, req.url),
      Number(target.status) || 301,
    );
    redirect.headers.set("Cache-Control", "public, max-age=0, s-maxage=30");
    return redirect;
  } catch {
    return null;
  }
}

export async function proxy(req: NextRequest) {
  return (await legacyRedirect(req)) ?? NextResponse.next();
}

A few decisions in there are worth spelling out.

  • The filter is narrow on purpose. A request with no query, or for a normal page, never makes the extra call. Normal pages keep their single, cacheable lookup.
  • It fails open. If Drupal is slow, down or has no redirect, the proxy returns null and the request carries on to the normal route, which will most likely show the 404 page. A redirect lookup should never be the reason a request errors. The three-second timeout keeps a slow backend from holding the request.
  • The status comes from Drupal. Editors choose 301 or 302 in the Redirect module, so the proxy uses that rather than hard-coding one.

I covered it with unit tests that build a NextRequest directly and stub fetch: one for the happy path (including that the lookup URL carries the encoded path and query), one asserting no call is made without a query or for a normal page, and one for Drupal failing or returning no redirect.

Why a deleted redirect kept working

Once the lookup was live in an environment, a redirect deleted in Drupal kept being served. There turned out to be two caches involved.

The CDN cached the 301

The first version of the redirect had no Cache-Control header. The CDN in front of the site treated the 301 as cacheable and kept answering it for minutes after the redirect was gone. Browsers are also allowed to cache a 301 heuristically when nothing says otherwise.

Purging by cache tag, the usual way to clear decoupled Drupal pages from a CDN, does not work here: the response is keyed on an arbitrary path plus query that no Drupal entity knows about, so there is nothing to purge by. The practical answer is a short shared lifetime and no browser caching: public, max-age=0, s-maxage=30. The CDN absorbs bursts of hits on a popular old link, and a change in Drupal shows up within half a minute.

Drupal's translate-path answer was cached too

Even with the redirect response fixed, the lookup could still return the deleted redirect. Drupal's translate-path response on its public route is cacheable at the edge for a day, and outside production nothing purged it when a redirect changed. cache: "no-store" only controls Next.js's own fetch cache; it says nothing to the CDN in front of Drupal.

The rest of the front end already handled this for page lookups by adding a cache-busting parameter to the translate-path call. The redirect lookup now sends the same parameter, so it always reaches Drupal. That is acceptable here only because the lookup is limited to a small share of traffic by the filter above. I would not bust the cache for every page request.

When this approach fits

I would reach for this whenever a decoupled site inherits old addresses where the query string carries the meaning: CMS pages keyed by id, search result URLs, or old .php scripts with parameters. If your legacy addresses are plain paths, you do not need any of it; the normal translate-path lookup already follows Drupal redirects.

Key takeaways

  • A Next.js catch-all route never sees the query string, so Drupal redirects that match on a query need a lookup in proxy.ts.
  • Limit the extra lookup to legacy file-like paths that carry a query, so normal pages keep a single cacheable request.
  • Make the lookup fail open with a short timeout, so a slow or failing backend never turns a redirect check into an error.
  • Give a redirect that cannot be purged by tag a short shared lifetime and max-age=0, or deleted redirects keep firing from the CDN and the browser.
  • cache: "no-store" on a fetch does not bypass a CDN in front of Drupal; check what caches the lookup itself before trusting it.

Written by Artem Shymko, a senior software engineer in Belgium working on Drupal, .NET and LLM features. Get in touch if you are working on something similar.