Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda