Managing URLs with redirects and rewrite paths
In this guide, you will learn how to configure redirects and rewrites in your FastStore project to manage users' access to your content.
Redirects are used to take users from one URL to another automatically. This is useful when a page has been moved, renamed, or removed. For example, if a page has been moved or renamed, the redirect ensures the user is taken to the new location.
Rewrites allow you to map custom URLs to existing page templates without changing the URL in the browser. This is useful when you want to hide complex or dynamic URL structures from users while maintaining clean and SEO-friendly URLs. For example, you can map a custom URL, such as
/sale, to an existing Product Listing Page (PLP) template.In summary:
- Redirects change both the URL the user sees and the destination.
- Rewrites keep the URL the same but change the destination internally.
These two tools enhance content organization, help maintain a good user experience, and ensure search engines continue to index the correct content.
Creating redirects
FastStore allows you to configure redirects through the
discovery.config.js file. This configuration is based on Next.js redirects.Projects hosted on Vercel have redirect limits such as, "Number of redirects in the array: 1,024 maximum" and "String length forsourceanddestinationparameters: 4.096 maximum." For more information, check the Vercel Redirects article.
-
Open your FastStore project in your preferred code editor and go to the
discovery.config.jsfile within your project directory. -
Inside the
discovery.config.js, add theredirectsfunction. This function will define your redirect rules, for example:_10account: "{accountName}",_10…_10async redirects() {_10const redirects = require('./src/redirects.json');_10return redirects;_10},Theredirectsfunction requires therequired.jsonfile, which you will create in the next step. This file contains the redirect definitions. -
Go to the
srcfolder and create theredirects.jsonfile. In theredirects.jsonfile, add the following:_10[_10{_10"source": "/office",_10"destination": "/about",_10"permanent": true_10}_10]Property name Description sourceOriginal URL pattern that triggers the redirect. In this example, any URL starting with /officewill be redirected.destinationNew URL to which users will be sent. Here, /aboutis the destination for users coming from/office.permanentThe type of redirect. Set to truefor a permanent (308 status code) redirect, which search engines will cache. Set tofalsefor a temporary (307 status code) redirect.For more information about each property, access the Next.js documentation. -
Run
yarn devto start your project in development mode. -
Open a browser and go to the old URL (e.g.,
/office). If the redirect is set up correctly, you should be automatically redirected to the new URL (e.g.,/about).
Creating rewrites
FastStore leverages Next.js rewrites to create custom routes while using existing Product Listing Page (PLP) templates. This allows you to map specific URLs to templates defined in Headless CMS, providing flexibility in content organization and URL structure.
For more information on using rewrites with PLP templates, check the Using rewrites with PLP template section on the Multiple page templates documentation.