# Redirects

Source: https://developers.swell.is/storefronts/redirects

Redirects send visitors from an old path on a storefront to its new address, so old links and search results keep working. A store import from another platform adds redirects from the old store's product and category URLs.

### Manage redirects

In the new Swell dashboard, go to **Online Store › Redirects** to add, edit, turn off or delete redirects. The classic dashboard has no Redirects page. Redirects are records in the `redirects` collection, so you can also manage them with the Backend API:

```javascript
await swell.post('/redirects', {
  from: '/collections/summer',
  to: '/categories/summer',
  type: 301,
  source: 'manual',
});
```

| Field | Description |
| --- | --- |
| from | Path a visitor requests, such as /collections/summer. Required. Stored lowercase, with one leading slash and no trailing slash, query or fragment. A full URL is stored as its path. Unique for each storefront. |
| to | Where the visitor is sent: a path on the storefront, such as /categories/summer, or a full URL. Required. |
| type | 301 for a permanent move, or 302 for a temporary one. Defaults to 301. |
| storefront_id | ID of the storefront the redirect applies to. Without one, the redirect applies to the primary storefront. |
| active | Indicates whether the redirect is used. Defaults to true. |
| source | Where the redirect came from: manual, or the platform of the store import that created it. A store import doesn't change a redirect whose source is manual. |
| migration_id | ID of the store import that created the redirect. |

### Patterns

A `*` in `from` matches any one part of a path. It must be a whole part, such as `/blog/*`, so `/blog-*` is refused. A pattern matches only paths with the same number of parts.

A `*` in `to` takes what a `*` in `from` matched. When `to` has fewer `*` than `from`, they take the last matches, in order. For example, a redirect from `/collections/*/products/*` to `/products/*` sends `/collections/sale/products/tee` to `/products/tee`. `to` can't have more `*` than `from`, and a redirect can't send a path to itself.

### How redirects are served

The storefront gateway answers a page request that has a redirect before the storefront app loads, on the storefront's Swell domain and on its custom domains. Only `GET` and `HEAD` requests are redirected. The home page and paths under `/api/` and `/checkout` are never redirected. When `to` is a path, the request's query string is kept.

A path's own redirect is used before any pattern, and patterns with fewer `*` are tried first. When the result of a pattern has its own redirect, the visitor is sent to that redirect's `to` in one step, so a product whose slug changed still lands on its page.

A storefront uses its own redirects first. The primary storefront also uses the redirects without a `storefront_id`. A store import adds its redirects without one, since an imported store's old domain points to the primary storefront.

Each environment has its own redirects. The gateway reads a store's active redirects once a minute, so a change can take up to a minute to apply. It reads up to 20,000 redirects for each environment.

### Headless storefronts

A headless storefront on its own hosting doesn't go through the storefront gateway, so it looks redirects up itself. The Frontend API returns a store's active redirects with their `from`, `to`, `type` and `storefront_id`, and can't change them. To find the redirect for a path, filter by `from` with the path in its stored form: lowercase, with no trailing slash or query.

```javascript
const { results } = await swell.get('/redirects', {
  from: '/collections/summer',
});

const redirect = results[0];
if (redirect) {
  // Respond with redirect.type (301 or 302) and a Location of redirect.to
}
```

A lookup by `from` finds exact paths only. To apply patterns, list the redirects and match them as the gateway does. On a store with several storefronts, use the redirects whose `storefront_id` is the storefront's own, and on the primary storefront also the redirects without one.

→ See [Headless storefronts](https://developers.swell.is/storefronts/headless-storefront-guide) for setting up a headless storefront.
