module.exports= {asyncrewrites() {return { beforeFiles: [// These rewrites are checked after headers/redirects// and before all files including _next/public files which// allows overriding page files { source:'/some-page', destination:'/somewhere-else', has: [{ type:'query', key:'overrideMe' }], }, ], afterFiles: [// These rewrites are checked after pages/public files// are checked but before dynamic routes { source:'/non-existent', destination:'/somewhere-else', }, ], fallback: [// These rewrites are checked after both pages/public files// and dynamic routes are checked { source:'/:path*', destination:`https://my-old-site.com/:path*`, }, ], } },}
module.exports= {asyncrewrites() {return [ { source:'/old-about/:path*', destination:'/about',// The :path parameter isn't used here so will be automatically passed in the query }, ] },}
如果在 destination 中使用了參數,則不會自動在查詢中傳遞任何參數。
next.config.js
module.exports= {asyncrewrites() {return [ { source:'/docs/:path*', destination:'/:path*',// The :path parameter is used here so will not be automatically passed in the query }, ] },}
module.exports= {asyncrewrites() {return [ { source:'/:first/:second', destination:'/:first?second=:second',// Since the :first parameter is used in the destination the :second parameter// will not automatically be added in the query although we can manually add it// as shown above }, ] },}
module.exports= {asyncrewrites() {return [ { source:'/blog/:slug', destination:'/news/:slug',// Matched parameters can be used in the destination }, ] },}
module.exports= {asyncrewrites() {return [ { source:'/blog/:slug*', destination:'/news/:slug*',// Matched parameters can be used in the destination }, ] },}
module.exports= {asyncrewrites() {return [ {// this will match `/english(default)/something` being requested source:'/english\\(default\\)/:slug', destination:'/en-us/:slug', }, ] },}
module.exports= {asyncrewrites() {return [// if the header `x-rewrite-me` is present,// this rewrite will be applied { source:'/:path*', has: [ { type:'header', key:'x-rewrite-me', }, ], destination:'/another-page', },// if the header `x-rewrite-me` is not present,// this rewrite will be applied { source:'/:path*', missing: [ { type:'header', key:'x-rewrite-me', }, ], destination:'/another-page', },// if the source, query, and cookie are matched,// this rewrite will be applied { source:'/specific/:path*', has: [ { type:'query', key:'page',// the page value will not be available in the// destination since value is provided and doesn't// use a named capture group e.g. (?<page>home) value:'home', }, { type:'cookie', key:'authorized', value:'true', }, ], destination:'/:path*/home', },// if the header `x-authorized` is present and// contains a matching value, this rewrite will be applied { source:'/:path*', has: [ { type:'header', key:'x-authorized', value:'(?<authorized>yes|true)', }, ], destination:'/home?authorized=:authorized', },// if the host is `example.com`,// this rewrite will be applied { source:'/:path*', has: [ { type:'host', value:'example.com', }, ], destination:'/another-page', }, ] },}
module.exports= {asyncrewrites() {return [ { source:'/blog', destination:'https://example.com/blog', }, { source:'/blog/:slug', destination:'https://example.com/blog/:slug',// Matched parameters can be used in the destination }, ] },}
module.exports= { basePath:'/docs',asyncrewrites() {return [ { source:'/with-basePath',// automatically becomes /docs/with-basePath destination:'/another',// automatically becomes /docs/another }, {// does not add /docs to /without-basePath since basePath: false is set// Note: this can not be used for internal rewrites e.g. `destination: '/another'` source:'/without-basePath', destination:'https://example.com', basePath:false, }, ] },}
module.exports= { i18n: { locales: ['en','fr','de'], defaultLocale:'en', },asyncrewrites() {return [ { source:'/with-locale',// automatically handles all locales destination:'/another',// automatically passes the locale on }, {// does not handle locales automatically since locale: false is set source:'/nl/with-locale-manual', destination:'/nl/another', locale:false, }, {// this matches '/' since `en` is the defaultLocale source:'/en', destination:'/en/another', locale:false, }, {// it's possible to match all locales even when locale: false is set source:'/:locale/api-alias/:path*', destination:'/api/:path*', locale:false, }, {// this gets converted to /(en|fr|de)/(.*) so will not match the top-level// `/` or `/fr` routes like /:path* would source:'/(.*)', destination:'/another', }, ] },}