Home > Blockchain >  What is the regular expression for all pages except "/"?
What is the regular expression for all pages except "/"?

Time:09-27

I am using NextAuth for Next.js for session management. In addition, I am using the middleware.js to protect my routes from unauthenticated users.

According to https://nextjs.org/docs/advanced-features/middleware#matcher, if we want to exclude a path, we do something like

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - static (static files)
     * - favicon.ico (favicon file)
     */
    '/((?!api|static|favicon.ico).*)',
  ],
}

In this example, we exclude /api, /static,/favicon.icon. However, I want to exclude all path except the home page, "/". What is the regular expression for that? I am tried '/(*)'. It doesn't seem to work.

CodePudding user response:

The regular expression which matches everything but a specific one-character string / is constructed as follows:

  • we need to match the empty string: empty regex.
  • we need to match all strings two characters long or longer: ..
  • we need to match one-character strings which are not that character: [^/].

Combining these three together with the | branching operator: "|.. |[^/]".

If we are using a regular expression tool that performs substring searching rather than a full match, we need to use its anchoring features; perhaps it supports the ^ and $ notation for that: "^(|.. |[^/])$".

I'm guessing that you might not want to match empty strings; in which case, revise your requirement and drop that branch from the expression.


Suppose we wanted to match all strings, except for a specific fixed word like abc. Without negation support in the regex language, we can use a generalization of the above trick.

  • Match the empty string, like before, if desired.
  • Match all one-character strings: .
  • Match all two-character strings: ..
  • Match all strings longer than three characters: ....

Those simple cases taken care of, we focus on matching just those three-symbol strings that are not abc. How can we do that?

  • Match all three-character strings that don't start with a: [^a]...
  • Match all three-character strings that don't have a b in the middle: .[^b].
  • Match all three-character strings that don't end in c: ..[^c].

Combine it all together: "|.|..|.... |[^a]..|.[^b].|..[^c]".

For longer words, we might want to take advantage of the {m,n} notation, if available, to express "match from zero to nine characters" and "match eleven or more characters".

CodePudding user response:

I will need to exclude the signin page and register page as well. Because, it will cause an infinite loop and an error, if you don't exclude signin page. For register page, you won't be able to register if you are redirected to the signin page.

So the "/", "/auth/signin", and "/auth/register" will be excluded. Here is what I needed:

export const config = { 
    matcher: [
        '/((?!auth).*)(. )'
    ] 
}
  • Related