Redirects send visitors and search engines from an old address on your [Static Hosting](Article: static-hosting) site to a new one. They are most useful after moving a site from another platform, such as WordPress, or after reorganising your pages, so that existing links and search rankings keep working.

This article also covers the **Trailing slash** setting, which decides whether your page addresses end in a slash.

## Two ways to add redirects

You can add redirect rules in two places, and use both on the same site:

* **A `_redirects` file** in your repository, deployed with your site. The rules are versioned with your code and take effect when you deploy.
* **Rules managed in DeployHQ**, from the **Redirects** tab of the server settings, the [Static Hosting Redirects API](Article: static-hosting-redirects-api), or the DeployHQ CLI. They take effect within about a minute, without a deployment, while the change propagates across Cloudflare's network.

Rules are checked from top to bottom, and the first rule that matches an address is used. The rules in the `_redirects` file are always checked first, so **the file wins**: when a rule in the file and a rule managed in DeployHQ both match an address, the file's rule is used.

DeployHQ tells you when this happens:

* In the **Redirects** tab, a rule managed in DeployHQ shows **Never applies** when a `_redirects` rule matches every address it does, or **Partly overridden** when a `_redirects` rule matches some of the same addresses. The note names the line of the `_redirects` file responsible, for example "your _redirects file, line 4". While **Advanced settings** is closed, its summary shows how many rules never apply.
* A rule managed in DeployHQ is also flagged when an earlier rule managed in DeployHQ matches every address it does, so it can never apply, for example a query rule placed below a rule for the same path without query parameters, or `/blog/news` placed below `/blog/*`. The note names the earlier rule by its number, for example "Never applies: rule 1 above (/old) matches every address this rule does, and it is checked first." Move the rule up, or change one of the rules. A rule that an earlier rule matches only some addresses of is not flagged, because a specific rule placed before a general one is the usual order.
* The deployment log lists the rules managed in DeployHQ that the deployed `_redirects` file overrides.

## Using a `_redirects` file

Create a plain text file named `_redirects` (no extension) and make sure it ends up at the root of the folder your server deploys, which is the **subdirectory to deploy from** in the server settings. DeployHQ reads it during each deployment that includes it and applies its rules to the site.

If your site is built by a framework, put the file where the framework copies files unchanged into the build output. For Astro, Vite, and many other frameworks that is the `public/` folder: `public/_redirects` ends up as `dist/_redirects`. A `_redirects` file in a subfolder of the deployed folder is treated as an ordinary file and is not read.

The file is read by a deployment that includes it: one where the file changed since the last deployment, or a [full deployment](Article: deploying-from-scratch) (deploying from scratch). A deployment that does not include it, including a plain redeploy, keeps the rules of the previous deployment. If you delete the file, its rules are removed on the next deployment.

A `_redirects` file that is a symbolic link, or is inside a symbolically linked folder, is not read: the deployment log says so and the previous rules stay in place. Commit it as a regular file.

If your site already deployed a `_redirects` file before redirects were available on Static Hosting, its rules are picked up the next time the file changes or on a full deployment. A plain redeploy does not pick them up. If the **_redirects file** tab does not list the rules of your `_redirects` file, run a full deployment once, or change the file.

## Managing rules in DeployHQ

Once your site has been created, open your project and go to the Static Hosting server's settings. In the **Site Configuration** card, open **Advanced settings**. It opens by itself when the site already has redirects, a `_redirects` file, or a Trailing slash setting other than the default; while it is closed, its summary lists those. It has three tabs:

* **URL handling**, with the [Trailing slash](#the-trailing-slash-setting) setting. On the new server form it is saved with the server; on an existing server the URL handling tab has its own **Save** button.
* **Redirects**, the rules managed in DeployHQ.
* **_redirects file**, the rules of the `_redirects` file of the active deployment, read-only.

The **Redirects** tab is a table you edit in place. Every cell is a field: change **From**, **Query (optional)**, **To**, or **Status** of any rule, type in the empty last row to add a rule, and use the delete button at the end of a row to remove one. To change the order, drag a rule by its number, or select the number and press the up or down arrow key (in any cell of the row, **Alt + Up** and **Alt + Down** do the same).

Your changes are kept on the page until you click **Save redirects**, which publishes all of them to your site at once; **Discard** puts the table back as it was. A bar shows how many changes are not saved yet, and your browser asks before you leave the page with unsaved changes. The rules are saved in DeployHQ, not in your repository.

When you save:

* If a rule has a problem, nothing is saved, and the problem is shown under that rule.
* If the rules were changed somewhere else since you opened the page, for example with the API or the CLI, nothing is saved, and you are asked to reload the page to see the latest rules.
* If the site is busy publishing a deployment or another change, nothing is changed and you are asked to try again in a moment.

The **_redirects file** tab lists the lines of the `_redirects` file of the active deployment in the order they run, each with its line number in the file. A line that was skipped is shown greyed out in its place, with the reason underneath, and a line that was applied in a different form has a note underneath. To change these rules, edit the file in your repository: they run before the rules managed in DeployHQ.

You can also manage these rules with the [Static Hosting Redirects API](Article: static-hosting-redirects-api) and with the `dhq servers redirects` commands of the DeployHQ CLI. See [Managing Servers with the CLI](Article: cli-servers).

## Syntax

Each line of a `_redirects` file is one rule:

```text
source [query parameters] destination [status]
```

Fields are separated by spaces. Blank lines are ignored, and everything from a `#` that starts a word to the end of the line is a comment. A rule managed in DeployHQ has the same parts: **From** is the source, **Query (optional)** the query parameters, and **To** the destination.

```text
# A page that moved
/old-page /new-page

# A temporary redirect
/summer-sale /sale 302

# Everything under /blog/ moves to /articles/
/blog/* /articles/:splat 301

# A named part of the address is reused in the destination
/users/:id /people/:id 301

# Send a section to another site
/docs/* https://docs.example.com/:splat 301

# Match a query parameter: /store?id=42 goes to /products/42
/store id=:id /products/:id 301
```

### Status codes

| Status | Meaning |
|--------|---------|
| `301` | Moved permanently. The default when no status is given. |
| `302` | Found (temporary). |
| `307` | Temporary redirect. |
| `308` | Permanent redirect. |

Use `301` or `308` for pages that moved for good, so search engines update their index, and `302` or `307` for temporary moves. A `!` after the status, as some other hosts use to force a rule, is accepted and ignored: every rule already applies even when a file exists at the source address.

### Sources

A source is a path starting with `/`. It can contain:

* **A splat**, `*`, which matches the rest of the address. Use it in the destination as `:splat`. A source can contain at most one `*`. `/blog/*` matches `/blog/2024/hello` (the splat is `2024/hello`) and also `/blog` itself.
* **Placeholders** such as `:id` or `:slug`, which match one part of the address between slashes, and can be reused in the destination. `/users/:id` matches `/users/42` but not `/users/42/posts`. Each part of the address between slashes can hold at most one placeholder or `*`, so write `/:year/:month` rather than `/:year-:month`.

Write sources as the addresses visitors see. Static Hosting shows pages without their `.html` extension, so a source such as `/about.html` is applied as `/about`, and a pattern ending in `.html`, such as `/docs/:page.html`, is skipped because it can never match.

Matching ignores upper and lower case, so `/About` matches a rule for `/about`.

### Destinations

A destination is either a path on your site, starting with `/`, or a full `http://` or `https://` address on another site. It can use the source's `:splat` and placeholders. For safety, a few destinations are not allowed:

* A destination that starts with `//` or `\`. To redirect to another site, use a full `https://` address.
* A destination that starts with `/:splat`. Add a path in front of it, for example `/blog/:splat`.
* A placeholder in the host name of a full address, such as `https://:splat/`.
* A port number in a full address.

Any query string on the original address is passed on to the destination: `/old-page?utm_source=newsletter` redirects to `/new-page?utm_source=newsletter`. If the destination has its own query string, such as `/new-page?ref=old`, that query string is used instead.

A redirect sends visitors to the destination exactly as written; nothing is added to the address. If a redirect would start a loop, or more than two redirects in a row on your site (a final redirect to another site does not count), the visitor gets an error page instead of the first redirect.

## Matching query parameters

Some addresses differ only by their query string, such as WordPress shortlinks like `/?p=123`. To match them, list the query parameters between the source and the destination as `name=value` pairs, separated by spaces. In the **Redirects** tab, put them in the **Query (optional)** field, for example `p=:id`.

```text
# /?p=123 goes to /posts/123
/ p=:id /posts/:id 301

# /?page=about goes to /about
/ page=about /about 301
```

The rule matches when:

* **Every listed parameter is present.** `/ p=:id ...` does not match `/` without a `p` parameter.
* **Each value matches.** A value is either an exact value, such as `page=about`, or a placeholder, such as `p=:id`, which matches any value made of letters, digits, `-`, `_`, `.`, and `~`. If the value contains anything else, the rule does not match.
* **Other parameters do not matter.** `/?p=123&utm_source=newsletter` still matches `/ p=:id ...`.

The parameters the rule matched are not passed on to the destination; any other parameters are. With the first rule above, `/?p=123&utm_source=newsletter` redirects to `/posts/123?utm_source=newsletter`.

A placeholder in the query cannot share its name with a placeholder in the source, and `:splat` cannot be used as a query placeholder.

A rule for the same path without query parameters matches every query string, so put query rules first: a query rule placed after a rule for the same path never applies. In a `_redirects` file it is skipped with a warning, and in the **Redirects** tab it is flagged.

## Trailing slashes in rules

Rules match an address with or without one trailing slash. A rule for `/old-post` matches both `/old-post` and `/old-post/`, and so does a rule written as `/old-post/`. You do not need two rules for the two versions. A request for either version, or for the same address ending in `.html`, is redirected once, with the rule's own status code.

A destination on your site follows your site's [Trailing slash](#the-trailing-slash-setting) setting automatically. For example, with **Add a trailing slash**, a rule to `/new-post` sends visitors straight to `/new-post/`, in a single redirect.

A rule that only adds or removes a trailing slash, such as `/about/ /about`, only takes effect when the Trailing slash setting is **Keep URLs as requested**. With the other options, the setting already does this for every page.

## Rollbacks

Rolling back a Static Hosting site to an earlier deployment also restores the `_redirects` rules of that deployment. Rules managed in DeployHQ are not affected by a rollback: they stay as they are now.

A deployment made before DeployHQ read your `_redirects` file, for example one made before redirects were available on Static Hosting, has no `_redirects` rules recorded. Rolling back to it leaves only the rules managed in DeployHQ.

## Limits

| Limit | Value |
|-------|-------|
| Size of the `_redirects` file | 1 MB. A larger file is not used at all, and a warning says so. |
| Rules in the `_redirects` file | Up to 50 by default. Only the first rules up to the limit are used, and a warning says how many were ignored. |
| Length of a line in the `_redirects` file | 2,048 bytes. Longer lines are skipped. |
| Rules managed in DeployHQ, per site | Up to 50 by default. |
| Changes to redirects, per account | 60 per hour, across all of the account's sites. Each **Save redirects** counts as one change, however many rules it changes. With the API or the CLI, adding, changing, reordering, or removing a rule each count as a change. Changing the Trailing slash setting counts as a change; saving the server without changing it does not. Only a change that is saved and published counts: a change that is refused or cannot be published does not, and neither does a **Save redirects** or a Trailing slash save that changes nothing. |

To stay within these limits, cover many addresses with one rule: a splat or a placeholder, such as `/blog/* /posts/:splat`, redirects a whole section of your site in a single rule.

## Skipped lines and warnings

A line of the `_redirects` file that cannot be used is skipped with a warning, and the rest of the file still applies. A problem in the file never fails a deployment.

Warnings appear in two places:

* **The deployment log** of the deployment that read the file. Up to 20 warnings are listed, followed by a count of any others.
* **The _redirects file tab** of the server settings, where each skipped line of the `_redirects` file of the active deployment is shown in its place, with its line number and the reason underneath. A problem with the whole file, such as a file that is too large, is shown above the list.

Some entries are notes about rules that were applied, rather than skipped. For example, a source written as `/about.html` is noted as applied as `/about`.

A rule managed in DeployHQ that has a problem is refused when you save it, with the reason. This includes a rule that redirects to itself or forms a loop with other rules managed in DeployHQ (a visit that would come back to an address it has already been to), and a rule with the same source and query parameters as another rule.

Some problems only show once rules are combined, so they are flagged under the rule in the **Redirects** tab rather than refused: a rule whose visit goes on through more than two redirects in a row on your site (a final redirect to another site does not count), counting the rules of your `_redirects` file ("This chain needs more than two redirects, so visitors would get an error."), and a rule that leads into a loop with your `_redirects` file. Visitors get an error page for these, so shorten the chain by pointing the first rule straight at the final address.

## What is not supported

These lines are skipped with a warning rather than applied differently from what they say:

* **Rewrites (status `200`) and proxying.** To serve `index.html` for every address of a Single Page Application, turn on **Single Page Application (SPA) mode** in the server settings instead. See [Static Hosting](Article: static-hosting). A line such as `/* /index.html 200` is skipped with the warning "Rewrites (status 200) are not supported, so this rule was skipped. To serve index.html for every path, turn on SPA mode."
* **Custom `404` rules**, and any status other than `301`, `302`, `307`, and `308`. Visitors to missing pages see the standard not-found page.
* **Conditions** such as `Country=`, `Language=`, `Role=`, and `Cookie=`.
* **Redirects from a whole domain**, such as a source of `https://old.example.com/*`. To send another domain to your site, see [Redirecting a domain](#redirecting-a-domain).

## Example: moving from WordPress to Astro

WordPress addresses usually end in a slash, such as `/hello-world/`. When you rebuild the site with Astro or another static site generator, set **Trailing slash** to **Add a trailing slash** so that your new pages keep the same addresses, then add a `_redirects` file for the addresses that changed.

Save this as `public/_redirects` in your Astro project:

```text
# WordPress shortlinks such as /?p=123
/ p=:id /posts/:id/ 301

# Category and tag archives
/category/* /blog/category/:splat 301
/tag/* /blog/tag/:splat 301

# The RSS feed
/feed/ /rss.xml 301

# An old permalink that moved
/2019/05/hello-world/ /blog/hello-world/ 301
```

With these rules:

* `/?p=123` redirects to `/posts/123/`.
* `/category/news/` redirects to `/blog/category/news/`.
* `/feed/` redirects to `/rss.xml`.
* `/2019/05/hello-world/` redirects to `/blog/hello-world/`, and so does `/2019/05/hello-world`.

The page destinations end in a slash to match the **Add a trailing slash** setting, although DeployHQ adds it for you if you leave it out. `/rss.xml` is a file, so it has none. After deploying, check the **_redirects file** tab and the deployment log for skipped lines.

## The Trailing slash setting

The **Trailing slash** setting, under **Advanced settings > URL handling** in the **Site Configuration** card of the server settings, decides what happens when a visitor asks for a page address with or without a slash at the end. You can choose it when you create the server, where it is saved with the server, and change it at any time afterwards. On an existing server the **URL handling** tab has its own **Save** button, like the **Redirects** tab, and a change is applied to your live site as soon as you save it, without a deployment. If the site is busy publishing a deployment or another change, the change cannot be published, or the account has reached its [limit of redirect changes](#limits), the setting is not changed and a message says why. Saving the behaviour the site already has publishes nothing.

| Option | What it does |
|--------|--------------|
| **Remove the trailing slash** (default) | `/about/` redirects to `/about`. |
| **Add a trailing slash** | `/about` redirects to `/about/`. Best when moving from WordPress. |
| **Keep URLs as requested** | `/about` and `/about/` both work. Add canonical tags to avoid duplicates in search. |

Files like `/logo.png` are never redirected, and neither is the home page `/`. With **Add a trailing slash** or **Keep URLs as requested**, an address such as `/about/` is also served from an `about.html` file when there is no `about/index.html`.

The setting can also be changed with the API and the CLI. See [Static Hosting Redirects API](Article: static-hosting-redirects-api) and [Managing Servers with the CLI](Article: cli-servers).

## Redirecting a domain

Redirect rules work on the path of an address on your site. To send visitors from another domain you own, such as an old domain after a rebrand, add it as a custom domain of the site: once the site's primary domain is active, every other active custom domain redirects to it, keeping the path and query string. See [Custom domains for Static Hosting](Article: static-hosting-custom-domains).
