rewrites
Add rewrites to your Next.js app.
Rewrites allow you to map an incoming request path to a different destination path.
Rewrites act as a URL proxy and mask the destination path, making it appear the user hasn't changed their location on the site. In contrast, redirects will reroute to a new page and show the URL changes.
To use rewrites you can use the rewrites
key in next.config.js
:
Rewrites are applied to client-side routing, a <Link href="/about">
will have the rewrite applied in the above example.
rewrites
is an async function that expects to return either an array or an object of arrays (see below) holding objects with source
and destination
properties:
source
:String
- is the incoming request path pattern.destination
:String
is the path you want to route to.basePath
:false
orundefined
- if false the basePath won't be included when matching, can be used for external rewrites only.locale
:false
orundefined
- whether the locale should not be included when matching.has
is an array of has objects with thetype
,key
andvalue
properties.missing
is an array of missing objects with thetype
,key
andvalue
properties.
When the rewrites
function returns an array, rewrites are applied after checking the filesystem (pages and /public
files) and before dynamic routes. When the rewrites
function returns an object of arrays with a specific shape, this behavior can be changed and more finely controlled, as of v10.1
of Next.js:
Good to know: rewrites in beforeFiles
do not check the filesystem/dynamic routes immediately after matching a source, they continue until all beforeFiles
have been checked.
The order Next.js routes are checked is:
- headers are checked/applied
- redirects are checked/applied
beforeFiles
rewrites are checked/applied- static files from the public directory,
_next/static
files, and non-dynamic pages are checked/served afterFiles
rewrites are checked/applied, if one of these rewrites is matched we check dynamic routes/static files after each matchfallback
rewrites are checked/applied, these are applied before rendering the 404 page and after dynamic routes/all static assets have been checked. If you use fallback: true/'blocking' ingetStaticPaths
, the fallbackrewrites
defined in yournext.config.js
will not be run.
Rewrite parameters
When using parameters in a rewrite the parameters will be passed in the query by default when none of the parameters are used in the destination
.
If a parameter is used in the destination none of the parameters will be automatically passed in the query.
You can still pass the parameters manually in the query if one is already used in the destination by specifying the query in the destination
.
Good to know: Static pages from Automatic Static Optimization or prerendering params from rewrites will be parsed on the client after hydration and provided in the query.
Path Matching
Path matches are allowed, for example /blog/:slug
will match /blog/hello-world
(no nested paths):
Wildcard Path Matching
To match a wildcard path you can use *
after a parameter, for example /blog/:slug*
will match /blog/a/b/c/d/hello-world
:
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
:
The following characters (
, )
, {
, }
, [
, ]
, |
, \
, ^
, .
, :
, *
, +
, -
, ?
, $
are used for regex path matching, so when used in the source
as non-special values they must be escaped by adding \\
before them:
Header, Cookie, and Query Matching
To only match a rewrite when header, cookie, or query values also match the has
field or don't match the missing
field can be used. Both the source
and all has
items must match and all missing
items must not match for the rewrite to be applied.
has
and missing
items can have the following fields:
type
:String
- must be eitherheader
,cookie
,host
, orquery
.key
:String
- the key from the selected type to match against.value
:String
orundefined
- the value to check for, if undefined any value will match. A regex like string can be used to capture a specific part of the value, e.g. if the valuefirst-(?<paramName>.*)
is used forfirst-second
thensecond
will be usable in the destination with:paramName
.
Rewriting to an external URL
Rewrites allow you to rewrite to an external URL. This is especially useful for incrementally adopting Next.js. The following is an example rewrite for redirecting the /blog
route of your main app to an external site.
If you're using trailingSlash: true
, you also need to insert a trailing slash in the source
parameter. If the destination server is also expecting a trailing slash it should be included in the destination
parameter as well.
Incremental adoption of Next.js
You can also have Next.js fall back to proxying to an existing website after checking all Next.js routes.
This way you don't have to change the rewrites configuration when migrating more pages to Next.js
Rewrites with basePath support
When leveraging basePath
support with rewrites each source
and destination
is automatically prefixed with the basePath
unless you add basePath: false
to the rewrite:
Rewrites with i18n support
When leveraging i18n
support with rewrites each source
and destination
is automatically prefixed to handle the configured locales
unless you add locale: false
to the rewrite. If locale: false
is used you must prefix the source
and destination
with a locale for it to be matched correctly.
Version History
Version | Changes |
---|---|
v13.3.0 | missing added. |
v10.2.0 | has added. |
v9.5.0 | Headers added. |