
To set up Next.js rewrites, you'll need to create a new file in the root of your project called `next.config.js`. This file is where you'll define your rewrites configuration.
The `next.config.js` file is where you'll import the `next` object and use its `rewrite` method to define your rewrites. For example, if you want to redirect all requests to `/old-page` to `/new-page`, you would use the following code: `module.exports = { rewrites: [{ source: '/old-page', destination: '/new-page' }] }`.
You can also use the `rewrites` option to specify a regex pattern for the source URL. This can be useful if you want to redirect multiple URLs to the same destination. For instance, if you want to redirect all URLs that start with `/old-path/` to `/new-path/`, you would use the following code: `module.exports = { rewrites: [{ source: '/old-path/(.*)', destination: '/new-path/$1' }] }`.
Recommended read: Nextjs Swc
How to Configure
To configure rewrites in Next.js, you'll need to create a next.config.js file in the root of your project.
If this caught your attention, see: Router Prefetch Next Js
This file is where you define your rewrites configuration, and it's essential for handling incoming requests and routing them to the correct pages or external resources.
To start, you'll need to export an object with a rewrites property in your next.config.js file. This property should be an array of rewrite objects, each with a source and destination property.
For example, you can use a rewrite to redirect any URLs that start with /old-blog/ to the corresponding URL in the new blog (/new-blog/). The :slug parameter will be preserved in the rewrite.
You can use any valid regular expression for the source property, and the destination property can be a string or a function that returns a string.
It's recommended to keep both the redirects and rewrites arrays under 1000 elements for better performance.
Here's a brief overview of the keys you can use in your rewrite objects:
- source: This is the URL that will be matched by the rewrite. You can use regular expressions here.
- destination: This is the URL that will be used as the destination for the rewrite. It can be a string or a function that returns a string.
- has: This key can be used to specify a condition for the rewrite to be applied. For example, you can use it to check if a page exists before applying the rewrite.
- missing: This key can be used to specify a condition for the rewrite to be applied when a page is missing.
Here's an example of how you can use the has and missing keys in your rewrite objects:
Rewrite Rules
A Next.js Rewrite is a configuration that maps an incoming request path to a different destination. This destination can be another path within your Next.js application or an external URL.
Rewrites are different from redirects in that they don’t change the browser’s URL, instead, they handle the request on the server-side and serve the appropriate content. This is a key distinction from redirects, which change the URL in the browser.
You can use regular expressions to match requests and rewrite them to different paths. For example, a rewrite can use a regular expression to match requests like /posts/2023/05/18/my-post and rewrites them to /archive/my-post.
Here are the options for redirects in Next.js:
- next.config.js
- Middleware
- getStaticProps/getServerSideProps for PageRoute
- redirect function for AppRouter
You can also use wildcard paths to match nested paths, or regex paths for more complex matching scenarios.
Basic Path Rewrite
Basic Path Rewrite is a fundamental concept in Next.js Rewrites. A rewrite maps an incoming request path to a different destination, serving the appropriate content without changing the browser's URL.
This process is handled on the server-side, ensuring a seamless user experience. A basic example of a rewrite maps an old blog post URL to a new one, where a request to /old-blog/hello-world is served by the page at /new-blog/hello-world.
Rewrites can be used to handle various scenarios, such as mapping old URLs to new ones, or serving cached pages for Incremental Static Regeneration (ISR). They can also be used to handle requests with dynamic parameters, preserving the original parameters.
Here are some key points to keep in mind when working with Basic Path Rewrite:
These examples illustrate how Basic Path Rewrite can be used to map different incoming request paths to different destinations. By understanding how to work with Basic Path Rewrite, you can create more efficient and user-friendly applications.
Rewriting to External URL
Rewriting to an external URL is a powerful feature in Next.js that allows you to map requests to a specific path to an external URL.
This can be done using the `Rewrite` method, as seen in Example 1, where requests to `/external` are mapped to the external URL `https://example.com`.
The user's browser will still display the original path, making it a seamless experience for the user. This is because Next.js rewrites handle the request on the server-side and serve the appropriate content, as explained in Understanding Rewrites.
You can use this feature to proxy requests to an external API, hiding the API's URL from the client, as shown in Example 4, where requests to `/api/some-endpoint` are proxied to `https://external-api.com/some-endpoint`.
This is useful when integrating with third-party services or when you're incrementally adopting a new backend, as mentioned in Static Files and External URLs.
See what others are reading: Api in App Router Next Js
Sources
- https://focusreactive.com/next-js-redirects/
- https://www.dhiwise.com/post/nextjs-rewrites-everything-you-need-to-know
- https://dev.to/daryllukas/configuring-rewrites-in-nextjs-470p
- https://devsarticles.com/how-to-rewrite-nextjs-complete-guide
- https://docs.netlify.com/frameworks/next-js/runtime-v4/redirects-and-rewrites/
Featured Images: pexels.com