Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Redirecting all paths on a Vercel instance#
I wanted to redirect all traffic to https://something.vercel.app/ to a different domain - preserving the path and the querystring and serving a 301 status code.
The Vercel redirects documentation doesn’t mention that you can capture patterns from the source and place them in the destination. The example they give is:
1{2 "redirects": [3 { "source": "/me", "destination": "/profile.html" },4 { "source": "/user", "destination": "/api/user", "permanent": false },5 { "source": "/view-source", "destination": "https://github.com/vercel/vercel" }6 ]7}It turns out you can use (.*) in the source and then $1 in the destination. Here’s the example that worked for me:
1{2 "redirects": [3 {4 "source": "/(.*)",5 "destination": "https://timezones-api.datasette.io/$1",6 "statusCode": 3017 }8 ]9}Vercel use 307 and 308 HTTP status codes. I prefer 301 and 302 which is why I used statusCode above instead of their "permanent": true.
Here’s how I deployed it:
1~ % cd /tmp2/tmp % mkdir timezones-api3/tmp % cd timezones-api4timezones-api % echo '{5 "redirects": [6 {7 "source": "/(.*)",8 "destination": "https://timezones-api.datasette.io/$1",9 "statusCode": 30110 }11 ]12}13' > vercel.json14timezones-api % vercel15Vercel CLI 19.2.016> NOTE: Deploying to Now 2.0 automatically. More: https://vercel.com/docs/version-detection17? Set up and deploy “/private/tmp/timezones-api”? [Y/n] y18? Which scope do you want to deploy to? simonw19? Found project “simonw/timezones-api”. Link to it? [Y/n] y20🔗 Linked to simonw/timezones-api (created .vercel and added it to .gitignore)21No framework detected. Default Project Settings:22- Build Command: `npm run vercel-build` or `npm run build`23- Output Directory: `public` if it exists, or `.`24- Development Command: None25? Want to override the settings? [y/N] y26? Which settings would you like to overwrite (select multiple)? None27🔍 Inspect: https://vercel.com/simonw/timezones-api-cn71cufah/simonw [1s]28✅ Production: https://timezones-api-simonw.vercel.app [copied to clipboard] [10s]29📝 Deployed to production. Run `vercel --prod` to overwrite later (https://vercel.link/2F).30💡 To change the domain or build command, go to https://vercel.com/simonw/timezones-api/settings