Skip to content

Commit fdc4c17

Browse files
authored
Add custom routes docs (rewrites, redirects, headers) (#14887)
Fixes #14452
1 parent 6fe1260 commit fdc4c17

File tree

4 files changed

+282
-0
lines changed

4 files changed

+282
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
description: Add custom HTTP headers to your Next.js app.
3+
---
4+
5+
# Headers
6+
7+
Headers allow you to set custom HTTP headers for an incoming request path.
8+
9+
To set custom HTTP headers you can use the `headers` key in `next.config.js`:
10+
11+
```js
12+
module.exports = {
13+
async headers() {
14+
return [
15+
{
16+
source: '/about',
17+
headers: [
18+
{
19+
key: 'x-custom-header',
20+
value: 'my custom header value',
21+
},
22+
{
23+
key: 'x-another-custom-header',
24+
value: 'my other custom header value',
25+
},
26+
],
27+
},
28+
,
29+
]
30+
},
31+
}
32+
```
33+
34+
`headers` is an async function that expects an array to be returned holding objects with `source` and `headers` properties:
35+
36+
- `source` is the incoming request path pattern.
37+
- `headers` is an array of header objects with the `key` and `value` properties.
38+
39+
## Path Matching
40+
41+
Path matches are allowed, for example `/blog/:slug` will match `/blog/hello-world` (no nested paths):
42+
43+
```js
44+
module.exports = {
45+
async headers() {
46+
return [
47+
{
48+
source: '/blog/:slug',
49+
headers: [
50+
{
51+
key: 'x-slug',
52+
value: ':slug', // Matched parameters can be used in the value
53+
},
54+
{
55+
key: 'x-slug-:slug', // Matched parameters can be used in the key
56+
value: 'my other custom header value',
57+
},
58+
],
59+
},
60+
,
61+
]
62+
},
63+
}
64+
```
65+
66+
### Wildcard Path Matching
67+
68+
To match a wildcard path you can use `*` after a parameter, for example `/blog/:slug*` will match `/blog/a/b/c/d/hello-world`:
69+
70+
```js
71+
module.exports = {
72+
async headers() {
73+
return [
74+
{
75+
source: '/blog/:slug*',
76+
headers: [
77+
{
78+
key: 'x-slug',
79+
value: ':slug*', // Matched parameters can be used in the value
80+
},
81+
{
82+
key: 'x-slug-:slug*', // Matched parameters can be used in the key
83+
value: 'my other custom header value',
84+
},
85+
],
86+
},
87+
,
88+
]
89+
},
90+
}
91+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
description: Add redirects to your Next.js app.
3+
---
4+
5+
# Redirects
6+
7+
Redirects allow you to redirect an incoming request path to a different destination path.
8+
9+
Redirects are only available on the Node.js environment and do not affect client-side routing.
10+
11+
To use Redirects you can use the `redirects` key in `next.config.js`:
12+
13+
```js
14+
module.exports = {
15+
async redirects() {
16+
return [
17+
{
18+
source: '/about',
19+
destination: '/',
20+
permanent: true,
21+
},
22+
]
23+
},
24+
}
25+
```
26+
27+
`redirects` is an async function that expects an array to be returned holding objects with `source`, `destination`, and `permanent` properties:
28+
29+
- `source` is the incoming request path pattern.
30+
- `destination` is the path you want to route to.
31+
- `permanent` if the redirect is permanent or not.
32+
33+
## Path Matching
34+
35+
Path matches are allowed, for example `/old-blog/:slug` will match `/old-blog/hello-world` (no nested paths):
36+
37+
```js
38+
module.exports = {
39+
async redirects() {
40+
return [
41+
{
42+
source: '/old-blog/:slug',
43+
destination: '/news/:slug', // Matched parameters can be used in the destination
44+
permanent: true,
45+
},
46+
]
47+
},
48+
}
49+
```
50+
51+
### Wildcard Path Matching
52+
53+
To match a wildcard path you can use `*` after a parameter, for example `/blog/:slug*` will match `/blog/a/b/c/d/hello-world`:
54+
55+
```js
56+
module.exports = {
57+
async redirects() {
58+
return [
59+
{
60+
source: '/blog/:slug*',
61+
destination: '/news/:slug*', // Matched parameters can be used in the destination
62+
permanent: true,
63+
},
64+
]
65+
},
66+
}
67+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
description: Add rewrites to your Next.js app.
3+
---
4+
5+
# Rewrites
6+
7+
Rewrites allow you to map an incoming request path to a different destination path.
8+
9+
Rewrites are only available on the Node.js environment and do not affect client-side routing.
10+
11+
To use rewrites you can use the `rewrites` key in `next.config.js`:
12+
13+
```js
14+
module.exports = {
15+
async rewrites() {
16+
return [
17+
{
18+
source: '/about',
19+
destination: '/',
20+
},
21+
]
22+
},
23+
}
24+
```
25+
26+
`rewrites` is an async function that expects an array to be returned holding objects with `source` and `destination` properties:
27+
28+
- `source` is the incoming request path pattern.
29+
- `destination` is the path you want to route to.
30+
31+
## Path Matching
32+
33+
Path matches are allowed, for example `/blog/:slug` will match `/blog/hello-world` (no nested paths):
34+
35+
```js
36+
module.exports = {
37+
async rewrites() {
38+
return [
39+
{
40+
source: '/blog/:slug',
41+
destination: '/news/:slug', // Matched parameters can be used in the destination
42+
},
43+
]
44+
},
45+
}
46+
```
47+
48+
### Wildcard Path Matching
49+
50+
To match a wildcard path you can use `*` after a parameter, for example `/blog/:slug*` will match `/blog/a/b/c/d/hello-world`:
51+
52+
```js
53+
module.exports = {
54+
async rewrites() {
55+
return [
56+
{
57+
source: '/blog/:slug*',
58+
destination: '/news/:slug*', // Matched parameters can be used in the destination
59+
},
60+
]
61+
},
62+
}
63+
```
64+
65+
## Rewriting to an external URL
66+
67+
<details>
68+
<summary><b>Examples</b></summary>
69+
<ul>
70+
<li><a href="https://github.com/vercel/next.js/tree/canary/examples/custom-routes-proxying">Incremental adoption of Next.js</a></li>
71+
</ul>
72+
</details>
73+
74+
Rewrites allow you to rewrite to an external url. This is especially useful for incrementally adopting Next.js.
75+
76+
```js
77+
module.exports = {
78+
async rewrites() {
79+
return [
80+
{
81+
source: '/blog/:slug',
82+
destination: 'https://example.com/blog/:slug', // Matched parameters can be used in the destination
83+
},
84+
]
85+
},
86+
}
87+
```
88+
89+
### Incremental adoption of Next.js
90+
91+
You can also make Next.js check the application routes before falling back to proxying to the previous website.
92+
93+
This way you don't have to change the rewrites configuration when migrating more pages to Next.js
94+
95+
```js
96+
module.exports = {
97+
async rewrites() {
98+
return [
99+
// we need to define a no-op rewrite to trigger checking
100+
// all pages/static files before we attempt proxying
101+
{
102+
source: '/:path*',
103+
destination: '/:path*',
104+
},
105+
{
106+
source: '/:path*',
107+
destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`,
108+
},
109+
]
110+
},
111+
}
112+
```

docs/manifest.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@
231231
"title": "Base Path",
232232
"path": "/docs/api-reference/next.config.js/basepath.md"
233233
},
234+
{
235+
"title": "Rewrites",
236+
"path": "/docs/api-reference/next.config.js/rewrites.md"
237+
},
238+
{
239+
"title": "Redirects",
240+
"path": "/docs/api-reference/next.config.js/redirects.md"
241+
},
242+
{
243+
"title": "Custom Headers",
244+
"path": "/docs/api-reference/next.config.js/headers.md"
245+
},
234246
{
235247
"title": "Custom Page Extensions",
236248
"path": "/docs/api-reference/next.config.js/custom-page-extensions.md"

0 commit comments

Comments
 (0)