Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!