# Redirects and rewrites (_redirects)

> Redirect old addresses, rewrite paths, serve clean URLs and add per-section 404 pages with a _redirects file in your site. Syntax, matching order, limits and examples.

Source: https://docs.evolvingedge.ai/redirects/

A `_redirects` file in your site tells the network what to do with addresses that have moved, or that don't match a file. It uses the same format as Netlify and Cloudflare Pages, so a file you already have usually works unchanged. Moving from Netlify with a `netlify.toml`? [`ee-migrate`](https://docs.evolvingedge.ai/migrate-from-netlify/) writes this file from it.

It applies to **Level 0 (public) sites only**. See [Scope and limits](#scope-and-limits).

## Where the file goes

Put `_redirects` in the **root of your build output**, next to your home page's `index.html`. Most generators copy a static folder into the output unchanged, so put the file there:

| Generator | Put `_redirects` in |
|---|---|
| Astro, Vite, Qwik City | `public/` |
| VitePress | `docs/public/` (your source folder's `public/`) |
| Hugo, Docusaurus, Gatsby | `static/` |
| Eleventy | Any folder you pass through to the output |
| Jekyll | The site root, with `include: [_redirects]` in `_config.yml` |

The file travels with your site. It deploys and rolls back with the content it belongs to.

## Syntax

One rule per line: the address to match, where to send it, and an optional status.

```
# Lines that start with # are comments
/old-page        /new-page/                    301
/blog/*          /news/:splat                  301
/:year/:slug     /posts/:slug                  301
/shop            https://shop.example.com/     302
```

- **`from`** is a path on your site, starting with `/`. It can also be a full URL on one of your domains, to redirect that domain: see [Redirect a whole domain](#redirect-a-whole-domain).
- **`to`** is a path on your site, or a full URL on another site.
- **Status** defaults to `301` if you leave it out.
- **`*`** at the end of `from` matches the rest of the path. Use `:splat` in `to` to carry it over.
- **`:name`** matches one path segment. Use the same `:name` in `to`.

## Statuses

| Status | What it does |
|---|---|
| `301` | Permanent redirect. The visitor's address bar changes. |
| `302` | Temporary redirect. The visitor's address bar changes. |
| `200` | **Rewrite.** Serves the target's content at the address that was asked for. The address bar doesn't change. |
| `404` | Serves the target's content with a 404 status. Use it for a not-found page for one section. See [Custom 404 pages](https://docs.evolvingedge.ai/custom-404/). |

`307` and `308` aren't supported.

A rewrite (`200`) can only serve a file from your own site. It can't point at another site.

## Which rule wins

1. **A real file wins over a rule.** If your site has a file at the requested path, it's served, and the rule is skipped. Add `!` to the status to force the rule to apply anyway:

   ```
   /old   /new/   301!
   ```

2. **Rules for a whole domain** (with a full URL as `from`) are checked first, on requests to that domain.
3. **Exact rules** (no `*` or `:name`) are checked next.
4. **Pattern rules** are checked in the order they appear in the file. The first match wins.
5. If two rules have the same `from`, the first one is used.

The first point is what makes a single-page-app fallback work. This rule sends every unknown path to your app, but still serves your real scripts, styles and images:

```
/*   /index.html   200
```

A path counts as having a real file if the file exists at that path, or if an `index.html` exists in a folder of that name.

## Routing settings

Three optional lines turn on behaviour for the whole site. Each is written as a comment, so the file still works on other hosts.

### Add a trailing slash

```
# ee:trailing-slash on
```

Many generators write each page as a folder with an `index.html` inside: `about/index.html`, served at `/about/`. Without this line, `/about` (no slash) is a 404. With it, `/about` redirects to `/about/`.

It applies to any path whose last part has no dot and no trailing slash. Your own rules are checked first, so it never adds an extra step in front of a redirect you wrote.

With Astro, also set `trailingSlash: 'always'` and `build: { format: 'directory' }` in `astro.config.mjs`, so your site's own links use the slash.

### Clean URLs

```
# ee:clean-urls on
```

Some generators write flat files (`about.html`) but link to `/about`. With this line, `/about` is served from `about.html`, and the address bar stays on `/about`.

Generators that need it: Quartz, Observable Framework, Hono's SSG helper, vite-ssg, Silex, îles and Emanote by default, and VitePress, Rspress, VuePress and HonKit when their `cleanUrls` option is on.

- **A real file still wins.** `/LICENSE` serves `LICENSE` even if `LICENSE.html` exists.
- **`about.html` counts as the page at `/about`.** A rule of your own for `/about` only applies if you force it with `!`.
- **With both settings on,** `/about` serves `about.html` when it exists, and redirects to `/about/` only when it doesn't.
- **`/about.html` still works** as well. Your generator's canonical tag tells search engines which address you prefer.

### Markdown for agents

```
# ee:markdown on
```

If your build writes a Markdown copy beside each page (`cli.md` beside `cli/index.html`), this line serves the copy from the page's own address to requests that ask for `text/markdown`. Browsers still get the HTML. See [Markdown for agents](https://docs.evolvingedge.ai/markdown-for-agents/).

## Redirect a whole domain

When your site moves to a new domain, a rule with a full URL as `from` sends every address on the old domain to the same address on the new one:

```
https://old.example.com/*   https://www.example.com/:splat   301!
```

The rule only applies to requests for `old.example.com`. On your other domains, your site works as before.

To set it up:

1. **Add a deployment target for the old domain** in the same project, and [claim the domain](https://docs.evolvingedge.ai/custom-domains/) as you would any other.
2. **Deploy the same build to both targets:** the one for your new domain and the one for the old domain. From the [command line](https://docs.evolvingedge.ai/cli/), run `ee-deploy` once with each `--domain`.
3. **Deploy to both again whenever you change the rules.** Each target serves the deploy it was last given.

**If the old domain only redirects**, it doesn't need your whole site. Deploy a folder holding just the rule in `_redirects` and an `index.html` to the old domain once, and keep deploying your site to the new one as before. For a large site, that saves uploading it twice on every deploy. [Redirect the root domain to www](https://docs.evolvingedge.ai/custom-domains/#redirect-the-root-domain-to-www) walks through it for a root domain.

Things to know:

- **Add `!`.** Without it, a page that exists on your site keeps loading on the old domain instead of moving.
- **Write `https://`.** The network moves every `http://` request to `https://` before any rule runs, so an `http://` rule on its own never applies. The deploy warns about one. A rule that only sends a domain from `http://` to `https://` on the same domain isn't needed.
- **Redirects only.** A rule for a domain can use `301` or `302`. It can't rewrite (`200`) or serve a `404`.
- **Moving one page on the old domain** works the same way, without the `*`:

  ```
  https://old.example.com/about   https://www.example.com/about-us/   301!
  ```

- **`www` to the bare domain:** `https://www.example.com/* https://example.com/:splat 301!`, with both domains claimed and deployed. See [Root domains](https://docs.evolvingedge.ai/custom-domains/#root-domains).
- **Rules for `*.netlify.app` don't apply here.** The deploy says so, and still goes ahead. You can remove them.
- **The deploy lists the domains your rules name,** for example `_redirects: domain-level rules for old.example.com; each fires only once that domain is claimed and bound to this site`. A rule for a domain you haven't claimed and deployed to does nothing.

## Common recipes

**Move one page:**

```
/pricing-2025   /pricing/   301
```

**Move a whole section, keeping the rest of each address:**

```
/blog/*   /news/:splat   301
```

**Move to a new domain:** see [Redirect a whole domain](#redirect-a-whole-domain).

```
https://old.example.com/*   https://www.example.com/:splat   301!
```

**Send a section to another site:**

```
/docs/*   https://docs.example.com/:splat   301
```

**A single-page app, where every unknown path loads the app:**

```
/*   /index.html   200
```

**A not-found page for one section:**

```
/docs/*   /docs/404.html   404
```

## Scope and limits

- **Level 0 sites only.** Encrypted sites (Level 1 and Level 2) don't read `_redirects`. See [encryption levels](https://docs.evolvingedge.ai/workloads/#encryption-levels-in-depth).
- **`307` and `308`** aren't supported.
- **Rewrites stay on your site.** A `200` rule that points at another site is refused.
- **Conditions aren't supported.** Netlify's `Country=`, `Language=`, `Role=` and `Cookie=` conditions, and rules that match on query parameters, stop the deploy.
- **Up to 2,000 rules** per file.
- **Redirects are cached for 5 minutes** by browsers, so a rule you remove stops applying soon after your next deploy.
- **The file isn't downloadable.** A request for `/_redirects` gets a 404.
- **Some paths belong to the network** and can't be redirected or used for your own files:
  - the files visitors' browsers use to load your site: `/ee.js`, `/ee-worker.js`, `/brotli_wasm.wasm`;
  - exactly `/health` and `/discover`;
  - `/_header`, `/_index`, `/_blocks`, `/_torrent`, `/_digest` and anything under `/_piece/`;
  - anything under `/.ee/`.

  Paths such as `/health/team`, `/discover/products` and `/ee/docs/` are yours.

## Checking your file

Your file is checked when you deploy, from the portal or the [command line](https://docs.evolvingedge.ai/cli/). The deploy stops, and names each line, if the file has:

- a rule with no target, or a target that isn't a path or a full URL;
- a status other than `301`, `302`, `200` or `404`;
- a rule that redirects to itself, or a chain of rules that loops, including a domain rule that sends its domain back to itself, or two domains that redirect to each other;
- a rewrite (`200`) or `404` rule whose target is on another site, or isn't a file in your site;
- a condition or a query-parameter match;
- a domain rule that rewrites (`200`) or serves a `404`, or whose `from` isn't a plain domain (no port, IP address or `*` in the domain);
- a rule for a path that belongs to the network;
- more than 2,000 rules.

Two rules with the same `from` only produce a warning, since the first one is used. So do an `http://` domain rule without an `https://` one, and a rule for a `*.netlify.app` address.

The deploy also prints what it found, for example `_redirects: 3 rule(s), trailing-slash: on, clean-urls: off, markdown: off`. Check that line if a routing setting doesn't seem to apply: a tool that strips comments can remove the setting without any other sign.

To check a rule after deploying, request the old address and look at the status and `Location`:

```sh
curl -sI https://www.example.com/old-page
```

**Next:** [Response headers (_headers) >](https://docs.evolvingedge.ai/headers/)
