Search forkrail

Search documentation, comparisons, and CLI commands.

Redirects, rewrites and headers

Route requests with vercel.json as you would on Vercel, keep up to a million bulk redirects, and change routing rules without a redeploy.

What runs, in order

Every request to a project goes through the same steps. The project’s Routing page in the dashboard draws them for your project, with links to each.

  1. The firewall and deployment protection.
  2. Bulk redirects: the project’s own, then the deployment’s.
  3. Routing rules, the project’s rules that change without a redeploy.
  4. The deployment’s vercel.json: cleanUrls and trailingSlash, then redirects, then headers.
  5. Middleware, on the paths it matches.
  6. The deployment’s files, then its rewrites, then its functions.

Routing in vercel.json

Every routing key of vercel.json works as it does on Vercel, for APIs, websites and static sites alike: redirects, rewrites, headers, cleanUrls, trailingSlash, and the older routes. A vercel.toml or vercel.ts works in its place (one of them per project).

vercel.json
{
"cleanUrls": true,
"trailingSlash": false,
"redirects": [
{ "source": "/old-blog/:slug", "destination": "/blog/:slug", "permanent": true }
],
"rewrites": [
{ "source": "/app/:path*", "destination": "/app/index.html" },
{ "source": "/api/:path*", "destination": "https://api.example.com/:path*" }
],
"headers": [
{ "source": "/(.*)", "headers": [{ "key": "X-Frame-Options", "value": "DENY" }] }
]
}

Patterns and conditions

Patterns are Vercel’s: /blog/:slug, /docs/:path*, /(.*), inline regexes such as /:id(\\d+), and lookaheads such as /((?!uk/).*). A rule can also require (has) or exclude (missing) a header, cookie, host or query parameter:

{
"redirects": [
{
"source": "/:path((?!beta/).*)",
"has": [{ "type": "cookie", "key": "beta", "value": "true" }],
"destination": "/beta/:path",
"permanent": false
}
]
}

Redirects

  • A redirect answers 308 by default. "permanent": false makes it 307, and statusCode can be 301, 302, 303, 307 or 308.
  • The visitor’s query string is kept, with the destination’s parameters set over it: /old?x=1 redirected to /new?from=old lands on /new?x=1&from=old.

Rewrites

  • A rewrite to one of the deployment’s files serves that file. A single-page app built with Vite needs one: { "source": "/(.*)", "destination": "/index.html" }.
  • A rewrite to a function (/api/main.py) runs it with the path the visitor asked for.
  • A rewrite to an https:// URL is proxied. The other site has 120 seconds to start answering, or the visitor gets 504 ROUTER_EXTERNAL_TARGET_ERROR.

Headers

Headers apply to whatever answers: a file, a function, an external site or the 404 page. A header your function sets itself wins over the same header from vercel.json.

Transforms and mitigations

A route in routes can change the request and the answer with Vercel’s transforms (request.headers, request.query, request.path, response.headers, each with set, append or delete), or answer with the firewall’s deny or challenge using "mitigate": { "action": "deny" }.

{
"routes": [{
"src": "/home",
"continue": true,
"transforms": [
{ "type": "request.query", "op": "set", "target": { "key": "theme" }, "args": "dark" },
{ "type": "response.headers", "op": "delete", "target": { "key": "x-powered-by" } }
]
}]
}
Mistakes are caught before upload

A pattern that doesn’t compile, a destination that uses a parameter its source doesn’t have, or permanent together with statusCode stops the deploy with a message, before anything is uploaded. The deploy prints how many rules it found, and each request’s log line names the rule that decided it, such as redirects[3].

Bulk redirects

For thousands of plain path-to-URL redirects, such as an old site’s pages, use bulk redirects: up to 1,000,000 a project, looked up in under a microsecond. Each has a source (a path, or a full URL to match one host), a destination, and optionally permanent, statusCode, caseSensitive and preserveQueryParams.

With the deployment

Name a CSV, JSON or JSONL file (or a folder of up to 100 of them) in vercel.json. The file is read after the build and isn’t served.

{ "bulkRedirectsPath": "redirects.csv" }

Per project, without a redeploy

Keep them on the project instead, under Routing → Bulk redirects or with forkrail redirects. Changes are staged as a new version and route nothing until you publish; publishing reaches every router within seconds, and older versions can be restored.

forkrail redirects add my-site /old /new --permanent
forkrail redirects import my-site redirects.csv # adds to the current ones; --replace for all
forkrail redirects test my-site https://my-site.example.com/old
forkrail redirects publish my-site # or: discard, versions, restore VERSION, export

An import through the dashboard or a signed-in CLI is at most 5 MB (about 50,000 redirects). With the install’s AWS credentials (--aws) it can be any size.

Routing rules

Routing rules are the project’s own rewrites, redirects and header changes, kept outside any deployment, so they apply to every deployment at once and change without a redeploy. Each matches a path (exactly, by a pattern like /blog/:slug, or by a regex), optionally a host, header, cookie or query, and then:

  • rewrites to a path or URL, redirects (301, 302, 303, 307 or 308) or answers with a status;
  • sets, appends or deletes response headers, request headers and query parameters.

The first rule with a rewrite, redirect or status decides; the header changes of every rule that matches apply. Edit them under Routing → Routing rules, or:

forkrail routes add my-site "API proxy" --src '/api/:path*' --src-syntax path-to-regexp \
--action rewrite --dest 'https://api.example.com/:path*'
forkrail routes add my-site "Old blog" --src /blog --src-syntax equals \
--action redirect --dest /articles --status 301
forkrail routes add my-site "Login" --src '/account/:path*' --src-syntax path-to-regexp \
--action redirect --dest /login --missing cookie:session
forkrail routes add my-site "CORS" --src '^/api/.*$' --set-response-header 'access-control-allow-origin=*'
forkrail routes test my-site /account/x -H 'cookie: session=1'
forkrail routes publish my-site

Like bulk redirects, every change is staged until you publish, with history and restore. Anyone in the team can look and test; members and owners can change them.

Limits

WhatLimit
Items in each vercel.json routing key2,048 (Vercel’s)
Length of a source or destination4,096 characters
Conditions in one has or missing16
Bulk redirects per project1,000,000
Bulk redirect import through a sign-in5 MB
External rewrite starts answering120 seconds

Cost: routing runs in the routers you already pay for. 2,048 rules add about 7 microseconds to a request.

New to forkrail? Install it in your AWS account and deploy your first app in about 20 minutes.
Quickstart