#next.js config
1 messages · Page 1 of 1 (latest)
In Next.js, the redirects and rewrites configuration in next.config.js supports path matching using parameters, wildcards, and regex. However, if you want to redirect or rewrite "everything except" a specific set of routes (i.e., an "all except" or blacklist approach), the only built-in way is to use regex in the source field, as you have shown in your example.
There is no built-in way in Next.js config to achieve this functionality without using regex or explicitly listing all allowed routes (a whitelist). The config API does not support a "catch-all except" pattern without regex or a whitelist. The documentation only describes path matching, wildcards, and regex for advanced matching scenarios, but does not mention any alternative for this use case without regex or explicit route listing (Path Matching in redirects).
If you want to avoid regex or whitelisting in the config, you would need to implement custom logic, such as using Middleware, where you can programmatically check the path and decide whether to redirect or not. Middleware allows you to write JavaScript logic to handle these cases, for example:
import { NextResponse } from 'next/server';
export function middleware(request) {
const { pathname } = request.nextUrl;
// List of routes to exclude from redirect
const excluded = [
'/route-1',
'/route-2',
'/favicon.ico',
'/robots.txt',
'/graphql',
'/.well-known'
];
// If the path is not in the excluded list, redirect to /route-1
if (!excluded.some(route => pathname.startsWith(route))) {
return NextResponse.redirect(new URL('/route-1', request.url), 308);
}
return NextResponse.next();
}
This approach gives you full control without regex in the config, but it does require using Middleware, not just the config file (Managing redirects at scale (advanced)).
Summary:
- The config file (
next.config.js) does not support "all except" matching without regex or a whitelist. - Use Middleware for programmatic, flexible control without regex in the config.
Let me know if you need an example for rewrites as well!
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@steel summit Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. Give me one moment! :books:
If you do not want to use Middleware, the only way to achieve "redirect/rewrite everything except certain routes" in Next.js config is by using a regex pattern in the source field of your redirects or rewrites in next.config.js. There is no built-in alternative to regex or explicit whitelisting in the config file for this use case.
The official documentation shows that negative matching (excluding certain paths) is only possible using regex patterns like negative lookaheads in the source field. For example:
module.exports = {
async redirects() {
return [
{
source: '/((?!route-1|route-2|favicon\\.ico|robots\\.txt|graphql|\\.well-known).*)',
destination: '/route-1',
permanent: true,
},
]
},
}
This pattern matches all paths except the ones you list. There is no config option to do this without regex or a whitelist approach (Path Matching in redirects).
In summary:
If you do not want to use Middleware, you must use regex or explicitly list all allowed routes in your config. There is no other built-in way to achieve this in Next.js config alone.
Path Matching in redirects
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@steel summit Kapa.ai is still learning and improving, please let me know how I did by reacting below.