Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
Redirect wildcard route in NextJS

In deploying a new NextJS app there are some legacy routes that need to be preserved from the old, non-NextJS site.

/const-string-[long-unique-hash] is used currently for a httpd conf redirect:

Ex. RewriteRule ^const-string-(.*)$ https://google.com?q=const-string-$1 [L,NC]

How can I preserve NextJS routing, but allow for legacy routes matching the path: /const-string-*?

Desired behavior:

  1. /blog routes pages/blog.js

  2. /const-string-a1b2c3d4 routes to https://google.com?q=const-string-a1b2c3d4

Current behavior (only localhost testing so far):

  1. /blog routes pages/blog.js works as expected

  2. /const-string-a1b2c3d4 routes to 404

How can I catch urls matching this string before it's redirected to 404?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For others that may wish to do the same, I ended up using a custom 404.js file to handle redirects like this.

pages/404.js:

import {useRouter} from 'next/router';
import { useEffect, useState } from 'react';
export default function Custom404() {
  const router = useRouter();
  const [route, setRoute] = useState('');

  var regex = new RegExp(/const-string-([0-9A-Za-z-]+)/gi);
  var matches = route.match(regex);
  useEffect(() => {
    setRoute(router.asPath);
    window.location.href = (matches ? "https://google.com/?q=" + matches[0] : '/?404');
  }, [router, matches]);

  return <h1>Redirecting...</h1>
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda