How to configure custom routing with config json on Vercel
Custom routing on Vercel is configured using a vercel.json file in your project root with routes array containing source patterns and destination targets. The configuration supports redirects, rewrites, headers, and dynamic routing patterns for full control over request handling.
Prerequisites
- Basic knowledge of JSON syntax
- Existing Vercel project or account
- Understanding of HTTP routing concepts
- Node.js project with package.json
Step-by-Step Instructions
Create vercel.json configuration file
vercel.json file in your project root directory. This file will contain all your custom routing configuration:{
"routes": [
{
"src": "/api/(.*)",
"dest": "/api/$1"
}
]
}The routes array is where you'll define all your custom routing rules.
Configure route patterns with source matching
{
"routes": [
{
"src": "/blog/(.*)",
"dest": "/blog.html?slug=$1"
},
{
"src": "/api/users/([^/]+)",
"dest": "/api/users.js?id=$1"
}
]
}Use
(.*) for catch-all patterns and ([^/]+) for single segments.Set up redirects and rewrites
{
"routes": [
{
"src": "/old-page",
"status": 301,
"headers": { "Location": "/new-page" }
},
{
"src": "/api/(.*)",
"dest": "/serverless-functions/$1"
}
]
}Use status and headers for redirects, or just dest for internal rewrites.
Add custom headers and CORS configuration
{
"routes": [
{
"src": "/api/(.*)",
"dest": "/api/$1",
"headers": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
"Cache-Control": "s-maxage=86400"
}
}
]
}This is essential for API routes that need CORS support or custom caching behavior.
Configure route priority and ordering
{
"routes": [
{
"src": "/api/users/profile",
"dest": "/api/profile.js"
},
{
"src": "/api/users/(.*)",
"dest": "/api/users.js?id=$1"
},
{
"src": "/api/(.*)",
"dest": "/api/$1"
}
]
}Vercel processes routes from top to bottom, stopping at the first match.
Test configuration locally
npm i -g vercel
vercel devNavigate to
http://localhost:3000 and test your custom routes. Check the terminal output for any routing errors or warnings. Verify that your patterns match the expected URLs and destinations work correctly.Deploy and verify routing configuration
vercel --prodAfter deployment, test all your custom routes in production. Check the Functions tab in your Vercel dashboard to see if serverless functions are being invoked correctly. Monitor the deployment logs for any routing-related errors.
Common Issues & Troubleshooting
Routes not working after deployment
Check that your vercel.json is in the project root and has valid JSON syntax. Verify route patterns using regex testing tools and ensure there are no conflicting routes above your configuration.
404 errors on dynamic routes
Ensure your destination files exist and your regex capture groups match properly. Check that $1, $2 variables correspond to your parentheses groups in the source pattern.
CORS errors on API routes
Add proper CORS headers to your route configuration: "Access-Control-Allow-Origin": "*" and "Access-Control-Allow-Methods": "GET, POST, OPTIONS". Handle preflight OPTIONS requests separately.
Redirects not preserving query parameters
Use $1 to capture and preserve query strings in your destination: "dest": "/new-path?$1" or modify your source pattern to include query parameter matching.