redirects
Add redirects to your Next.js app.
Redirects allow you to redirect an incoming request path to a different destination path.
To use redirects you can use the redirects key in next.config.js:
redirects is an async function that expects an array to be returned holding objects with source, destination, and permanent properties:
sourceis the incoming request path pattern.destinationis the path you want to route to.permanenttrueorfalse- iftruewill use the 308 status code which instructs clients/search engines to cache the redirect forever, iffalsewill use the 307 status code which is temporary and is not cached.
Why does Next.js use 307 and 308? Traditionally a 302 was used for a temporary redirect, and a 301 for a permanent redirect, but many browsers changed the request method of the redirect to GET, regardless of the original method. For example, if the browser made a request to POST /v1/users which returned status code 302 with location /v2/users, the subsequent request might be GET /v2/users instead of the expected POST /v2/users. Next.js uses the 307 temporary redirect, and 308 permanent redirect status codes to explicitly preserve the request method used.
basePath:falseorundefined- if false thebasePathwon't be included when matching, can be used for external redirects only.locale:falseorundefined- whether the locale should not be included when matching.hasis an array of has objects with thetype,keyandvalueproperties.missingis an array of missing objects with thetype,keyandvalueproperties.
Redirects are checked before the filesystem which includes pages and /public files.
When using the Pages Router, redirects are not applied to client-side routing (Link, router.push) unless Proxy is present and matches the path.
When a redirect is applied, any query values provided in the request will be passed through to the redirect destination. For example, see the following redirect configuration:
Good to know: Remember to include the forward slash / before the colon : in path parameters of the source and destination paths, otherwise the path will be treated as a literal string and you run the risk of causing infinite redirects.
When /old-blog/post-1?hello=world is requested, the client will be redirected to /blog/post-1?hello=world.
Path Matching
Path matches are allowed, for example /old-blog/:slug will match /old-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 parentheses after a parameter, for example /post/:slug(\\d{1,}) will match /post/123 but not /post/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 redirect 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 redirect 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:Stringorundefined- 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-secondthensecondwill be usable in the destination with:paramName.
Redirects with basePath support
When leveraging basePath support with redirects each source and destination is automatically prefixed with the basePath unless you add basePath: false to the redirect:
Redirects with i18n support
When implementing redirects with internationalization in the App Router, you can include locales in next.config.js redirects, but only as hardcoded paths.
For dynamic or per-request locale handling, use dynamic route segments and proxy, which can redirect based on the user's preferred language.
In some rare cases, you might need to assign a custom status code for older HTTP Clients to properly redirect. In these cases, you can use the statusCode property instead of the permanent property, but not both. To ensure IE11 compatibility, a Refresh header is automatically added for the 308 status code.
Other Redirects
- Inside API Routes and Route Handlers, you can redirect based on the incoming request.
- Inside
getStaticPropsandgetServerSideProps, you can redirect specific pages at request-time.
Version History
| Version | Changes |
|---|---|
v13.3.0 | missing added. |
v10.2.0 | has added. |
v9.5.0 | redirects added. |