question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regex Path Matching for Redirects

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

A common URL pattern for blogs and websites coming from Wordpress is /year/month/day/slug. To redirect pages that match this to a different page, the path matching would look like this:

{
  source: '/:year/:month/:day/:post',
  destination: '/blog/:post',
  permanent: true,
},

This however matches generated Next.js files (for example, /_next/static/css/b9c315e24bcc3c2b5b54.css which results in effectively breaking the built website (dev mode doesn’t match this pattern).

Describe the solution you’d like

By adding more finegrained matchers like Regex strings, you could restrict the matching to look for numbers only. Using the example above, in the vercel.json file I can currently write "\/\\d{4}\/\\d{2}\/\\d{2}\/(.+)\/?" to only match blog posts.

Describe alternatives you’ve considered

I can achieve this by leveraging platform-specific files such as vercel.json but it would be nice to have support for this out of the box!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
ijjkcommented, Jul 30, 2020

Hi, this is supported in Next.js, see the example redirect below for matching all routes except _next. Note: the rewrites and redirects support in vercel.json matches Next.js’ built-in support

module.exports = {
  redirects() {
    return [
      {
        source: '/:year((?!_next).*)/:month/:day/:post',
        destination: '/blog/:post',
        permanent: false
      }
    ]
  }
}
1reaction
ijjkcommented, Jul 30, 2020

It does look like this isn’t documented, I opened a PR to mention this support here, thanks for bringing this up!

Read more comments on GitHub >

github_iconTop Results From Across the Web

RegEx for Redirect Rules and SSL - Support Center
A “Regular Expression”, or RegEx, is a set of characters that create a search pattern. It's useful for redirect and SSL rules because...
Read more >
Redirect Regular Expressions
A regex URL can match many URLs. In addition to matching many URLs, a regular expression can extract information from the source URL...
Read more >
What are REGEX redirects? • Yoast
These are redirects that use Regular Expressions to match multiple source URLs and redirect all of them to the destination.
Read more >
How to Use Regex Redirects in Rank Math
Add your source URLs and select the matching option as Regex from the drop-down list. Add your destination URL, and make sure you...
Read more >
RegEx matching for URL redirect in htaccess - Stack Overflow
RewriteRule matches against the path component of the URL only, if you want to check or match query string contents, you need to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found