Unverified Commit 766fef22 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Document regex support for custom routes (#15714)

This adds an example of using regex matching for custom routes

Fixes https://github.com/vercel/next.js/issues/15712
parent 9d68a363
...@@ -90,6 +90,28 @@ module.exports = { ...@@ -90,6 +90,28 @@ module.exports = {
} }
``` ```
### Regex Path Matching
To match a regex path you can wrap the regex in parenthesis after a parameter, for example `/blog/:slug(\\d{1,})` will match `/blog/123` but not `/blog/abc`:
```js
module.exports = {
async rewrites() {
return [
{
source: '/blog/:post(\\d{1,})',
headers: [
{
key: 'x-post',
value: ':post',
},
],
},
]
},
}
```
### Headers with basePath support ### Headers with basePath support
When leveraging [`basePath` support](/docs/api-reference/next.config.js/basepath.md) with headers each `source` is automatically prefixed with the `basePath` unless you add `basePath: false` to the header: When leveraging [`basePath` support](/docs/api-reference/next.config.js/basepath.md) with headers each `source` is automatically prefixed with the `basePath` unless you add `basePath: false` to the header:
......
...@@ -66,6 +66,24 @@ module.exports = { ...@@ -66,6 +66,24 @@ module.exports = {
} }
``` ```
### Regex Path Matching
To match a regex path you can wrap the regex in parenthesis after a parameter, for example `/blog/:slug(\\d{1,})` will match `/blog/123` but not `/blog/abc`:
```js
module.exports = {
async redirects() {
return [
{
source: '/old-blog/:post(\\d{1,})',
destination: '/blog/:post', // Matched parameters can be used in the destination
permanent: false,
},
]
},
}
```
### Redirects with basePath support ### Redirects with basePath support
When leveraging [`basePath` support](/docs/api-reference/next.config.js/basepath.md) with redirects each `source` and `destination` is automatically prefixed with the `basePath` unless you add `basePath: false` to the redirect: When leveraging [`basePath` support](/docs/api-reference/next.config.js/basepath.md) with redirects each `source` and `destination` is automatically prefixed with the `basePath` unless you add `basePath: false` to the redirect:
......
...@@ -69,6 +69,23 @@ module.exports = { ...@@ -69,6 +69,23 @@ module.exports = {
} }
``` ```
### Regex Path Matching
To match a regex path you can wrap the regex in parenthesis after a parameter, for example `/blog/:slug(\\d{1,})` will match `/blog/123` but not `/blog/abc`:
```js
module.exports = {
async rewrites() {
return [
{
source: '/old-blog/:post(\\d{1,})',
destination: '/blog/:post', // Matched parameters can be used in the destination
},
]
},
}
```
## Rewriting to an external URL ## Rewriting to an external URL
<details> <details>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment