The Static Hosting Redirects API lets you list, add, change, reorder, and remove the redirect rules of a [Static Hosting](Article: static-hosting) site, and set how the site handles trailing slashes. For how redirects work and the full rule syntax, see [Redirects for Static Hosting](Article: static-hosting-redirects).

The rules managed with this API are the ones shown in the **Redirects** tab, under **Advanced settings** in the **Site Configuration** card of the server settings. A site can also have rules in a `_redirects` file deployed with it. The file's rules are checked first and win, so this API also returns them, read-only, so you can see which of your rules they override. To change the file's rules, edit the file and deploy.

Every change is published to the site as soon as the request succeeds, and reaches the live site within about a minute while it propagates across Cloudflare's network.

Every endpoint is under the Static Hosting server's path, `/projects/:project_id/servers/:server_id/redirect_rules`. Replace `:project_id` with the permalink or identifier of the project, and `:server_id` with the identifier of the Static Hosting server. Static Hosting sites are created inside a project; they can't be global servers.

Every endpoint needs permission to edit the project's servers. A server that is not a Static Hosting server, or whose site has not been created yet, returns HTTP `404`.

**Tip**: You can explore and test these endpoints interactively using our [OpenAPI documentation](https://api.deployhq.com/docs).

# List redirect rules

Returns the redirect rules managed in DeployHQ, in the order they are checked, and the rules of the active deployment's `_redirects` file.

## URL

```text
/projects/:project_id/servers/:server_id/redirect_rules
```

## HTTP Method

```text
GET
```

## Example cURL request

```bash
curl -H "Content-type: application/json" \
-H "Accept: application/json" \
--user user@example.com:api-key \
https://test.deployhq.com/projects/my-project/servers/a1b2c3d4-e5f6-7890-abcd-ef1234567890/redirect_rules
```

## Example Response

```json
{
  "rules": [
    {
      "identifier": "5f0c2a1e-8b3d-4c6f-9e7a-1d2b3c4d5e6f",
      "position": 1,
      "source": "/old-page",
      "destination": "/new-page",
      "status": 301,
      "query": null,
      "shadowed_by": null
    },
    {
      "identifier": "9a1b7c3d-2e4f-4a6b-8c0d-3e5f7a9b1c2d",
      "position": 2,
      "source": "/blog/*",
      "destination": "/articles/:splat",
      "status": 301,
      "query": null,
      "shadowed_by": {
        "source": "/blog/*",
        "line": 3,
        "rule_identifier": null
      }
    },
    {
      "identifier": "2d4f6a8b-0c1e-4f3a-9b5d-7e9f1a3c5b7d",
      "position": 3,
      "source": "/old-page",
      "destination": "/somewhere-else",
      "status": 301,
      "query": "ref=:code",
      "shadowed_by": {
        "source": "/old-page",
        "line": null,
        "rule_identifier": "5f0c2a1e-8b3d-4c6f-9e7a-1d2b3c4d5e6f"
      }
    }
  ],
  "file_rules": [
    {
      "source": "/",
      "destination": "/posts/:id",
      "status": 301,
      "query": "p=:id",
      "line": 2
    },
    {
      "source": "/blog/*",
      "destination": "/news/:splat",
      "status": 301,
      "query": null,
      "line": 3
    }
  ],
  "file_warnings": [
    {
      "line": 5,
      "code": "rewrite_unsupported",
      "reason": "Rewrites (status 200) are not supported, so this rule was skipped. To serve index.html for every path, turn on SPA mode.",
      "text": "/* /index.html 200"
    }
  ],
  "deployment_uuid": "3c9e1f20-7a4b-4d8e-b5c6-0f1a2b3c4d5e"
}
```

* `rules` are the rules managed in DeployHQ. `shadowed_by` names the rule that takes over addresses the rule matches, and is `null` when there is none. It is either:
  * a `_redirects` rule that matches some or all of the same addresses, with its `source` and `line`, and `rule_identifier` set to `null`; or
  * an earlier rule managed in DeployHQ that matches every address the rule does, so the rule can never apply, with its `source` and `rule_identifier`, and `line` set to `null`. In the example, the third rule never applies because the first rule, for the same path without query parameters, matches every query string. An earlier rule that matches only some of the same addresses is not reported.
* `file_rules` are the rules of the `_redirects` file of the active deployment, in file order, with their line numbers.
* `file_warnings` are the lines of that file that were skipped, each with a `code`, a readable `reason`, and `text`, the line as written (up to 200 characters; `null` for a warning about the whole file, or for a file read before the line text was recorded). A few entries are notes about rules that were applied, such as `source_normalised` (a source written as `/about.html` was applied as `/about`) and `slash_only_redirect` (a rule that only adds or removes a trailing slash). A warning about the whole file, such as a file larger than 1 MB, has a `line` of `null`. At most 20 warnings are returned.
* `deployment_uuid` is the active deployment the file rules come from. It is `null` before the site's first deployment, and `file_rules` and `file_warnings` are then empty.

# Add a redirect rule

Adds a redirect rule and publishes the site's rules.

## URL

```text
/projects/:project_id/servers/:server_id/redirect_rules
```

## HTTP Method

```text
POST
```

## Parameters

| Parameter | Description |
|-----------|-------------|
| `redirect_rule[source]` | The path to redirect from, starting with `/`. It can use a `*` splat and `:name` placeholders. Required. |
| `redirect_rule[destination]` | A path on the site, or a full `http://` or `https://` address. It can use the source's `:splat` and placeholders, and the query placeholders. Required. |
| `redirect_rule[status]` | `301` (the default), `302`, `307`, or `308`. Optional. |
| `redirect_rule[query]` | Query parameters to match as well as the path, as space-separated `name=value` pairs, where a value is an exact value or a `:name` placeholder (for example `p=:id`). Optional; omit it to match on the path only. |
| `redirect_rule[position]` | Where to insert the rule, where `1` is checked first. Optional; by default the rule is added last. A position past the end adds the rule last. |

With `query`, every listed parameter must be present for the rule to match, other parameters do not stop the match, and the matched parameters are not passed on to the destination. See the matching query parameters section of [Redirects for Static Hosting](Article: static-hosting-redirects).

The source is stored as visitors see it: a source such as `/about.html` is saved as `/about`.

## Example cURL request

```bash
curl -X POST \
-H "Content-type: application/json" \
-H "Accept: application/json" \
--user user@example.com:api-key \
-d '{"redirect_rule":{"source":"/","query":"p=:id","destination":"/posts/:id","status":301}}' \
https://test.deployhq.com/projects/my-project/servers/a1b2c3d4-e5f6-7890-abcd-ef1234567890/redirect_rules
```

## Example Response

A successful request returns HTTP `201`:

```json
{
  "identifier": "c7d8e9f0-1a2b-4c3d-8e4f-5a6b7c8d9e0f",
  "position": 3,
  "source": "/",
  "destination": "/posts/:id",
  "status": 301,
  "query": "p=:id",
  "shadowed_by": null
}
```

# Update a redirect rule

Changes a redirect rule and publishes the site's rules. Only the fields you send are changed.

## URL

```text
/projects/:project_id/servers/:server_id/redirect_rules/:identifier
```

Replace `:identifier` with the `identifier` of the rule.

## HTTP Method

```text
PATCH
```

## Parameters

The same parameters as [Add a redirect rule](#add-a-redirect-rule), all optional. Send `query` as `null` or an empty string to make the rule match on the path only again. Send `position` to move the rule.

## Example cURL request

```bash
curl -X PATCH \
-H "Content-type: application/json" \
-H "Accept: application/json" \
--user user@example.com:api-key \
-d '{"redirect_rule":{"destination":"/new-home","status":302}}' \
https://test.deployhq.com/projects/my-project/servers/a1b2c3d4-e5f6-7890-abcd-ef1234567890/redirect_rules/5f0c2a1e-8b3d-4c6f-9e7a-1d2b3c4d5e6f
```

## Example Response

A successful request returns HTTP `200`, with the rule in the same format as [Add a redirect rule](#add-a-redirect-rule):

```json
{
  "identifier": "5f0c2a1e-8b3d-4c6f-9e7a-1d2b3c4d5e6f",
  "position": 1,
  "source": "/old-page",
  "destination": "/new-home",
  "status": 302,
  "query": null,
  "shadowed_by": null
}
```

# Remove a redirect rule

Removes a redirect rule and publishes the site's rules. The rule stops applying within about a minute, once the change has propagated.

## URL

```text
/projects/:project_id/servers/:server_id/redirect_rules/:identifier
```

## HTTP Method

```text
DELETE
```

## Example cURL request

```bash
curl -X DELETE \
-H "Content-type: application/json" \
-H "Accept: application/json" \
--user user@example.com:api-key \
https://test.deployhq.com/projects/my-project/servers/a1b2c3d4-e5f6-7890-abcd-ef1234567890/redirect_rules/5f0c2a1e-8b3d-4c6f-9e7a-1d2b3c4d5e6f
```

## Example Response

A successful request returns HTTP `204` with no body.

# Reorder redirect rules

Sets the order in which the redirect rules are checked, and publishes them. The first rule that matches an address is used, so put specific rules before general ones.

## URL

```text
/projects/:project_id/servers/:server_id/redirect_rules/reorder
```

## HTTP Method

```text
PUT
```

## Parameters

| Parameter | Description |
|-----------|-------------|
| `identifiers` | The identifiers of every rule of the site, each exactly once, in the new order. The first is checked first. Required. |

## Example cURL request

```bash
curl -X PUT \
-H "Content-type: application/json" \
-H "Accept: application/json" \
--user user@example.com:api-key \
-d '{"identifiers":["9a1b7c3d-2e4f-4a6b-8c0d-3e5f7a9b1c2d","5f0c2a1e-8b3d-4c6f-9e7a-1d2b3c4d5e6f","c7d8e9f0-1a2b-4c3d-8e4f-5a6b7c8d9e0f"]}' \
https://test.deployhq.com/projects/my-project/servers/a1b2c3d4-e5f6-7890-abcd-ef1234567890/redirect_rules/reorder
```

## Example Response

A successful request returns HTTP `200`, with the rules in their new order:

```json
{
  "rules": [
    {
      "identifier": "9a1b7c3d-2e4f-4a6b-8c0d-3e5f7a9b1c2d",
      "position": 1,
      "source": "/blog/*",
      "destination": "/articles/:splat",
      "status": 301,
      "query": null,
      "shadowed_by": null
    },
    {
      "identifier": "5f0c2a1e-8b3d-4c6f-9e7a-1d2b3c4d5e6f",
      "position": 2,
      "source": "/old-page",
      "destination": "/new-page",
      "status": 301,
      "query": null,
      "shadowed_by": null
    },
    {
      "identifier": "c7d8e9f0-1a2b-4c3d-8e4f-5a6b7c8d9e0f",
      "position": 3,
      "source": "/",
      "destination": "/posts/:id",
      "status": 301,
      "query": "p=:id",
      "shadowed_by": null
    }
  ]
}
```

If `identifiers` does not list every rule of the site exactly once, nothing is changed and the API returns HTTP `422`:

```json
{
  "errors": ["List every redirect rule of the site exactly once, in the new order."]
}
```

# Errors

If a rule is refused, the API returns HTTP `422` with a list of error messages, and nothing is changed:

```json
{
  "errors": ["Another redirect rule already redirects /old-page."]
}
```

A rule is refused when, for example:

* the source does not start with `/`, contains `?` or `#`, has more than one `*`, or has more than one placeholder or `*` between two slashes (such as `/:year-:month`);
* the destination is not a path or a full `http://` or `https://` address, or uses a placeholder the source and query do not define;
* the status is not `301`, `302`, `307`, or `308`;
* it redirects to itself, or creates a redirect loop with the site's other rules managed in DeployHQ (a visit that would come back to an address it has already been to);
* another rule already has the same source and query parameters;
* the site already has the most rules it can have (50 by default).

Because the first rule that matches an address is used, a loop can also come from the order of the rules. A reorder, an update, or a removal that would leave the site's rules in a loop is refused with HTTP `422` too, for example removing a rule that sent an address off the site while two other rules redirect to each other through it.

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 the `_redirects` file, is not refused, but visitors get an error page for it instead of the first redirect. The **Redirects** tab of the server settings flags such a rule; point it straight at the final address instead.

While the site is being deleted, every change returns HTTP `422`:

```json
{
  "errors": ["This site is being deleted"]
}
```

While the site is busy publishing a deployment or another change, nothing is changed and the API returns HTTP `409`. Try again shortly:

```json
{
  "error": "The site is busy publishing a deployment or another change, so nothing was changed. Try again in a moment."
}
```

If the rules cannot be published to the site, nothing is changed and the API returns HTTP `503`. Try again in a moment:

```json
{
  "error": "The redirect rules could not be published, so nothing was changed. Try again in a moment."
}
```

# Rate limit

An account can make at most 60 redirect changes per hour, across all of its sites. Adding, updating, reordering, and removing a rule each count as one change once it is applied. A request that is refused or fails, for example with `422`, `409`, or `503`, does not count. Changing a site's [trailing slash setting](#trailing-slash-setting) also counts, but a server update that leaves the setting as it is does not. Listing rules does not count.

Over the limit, the API returns HTTP `429`, with a matching `Retry-After` header. `retry_after` is the longest you may need to wait, in seconds:

```json
{
  "error": "Too many redirect changes (adding, changing, removing or reordering redirect rules, or changing the trailing slash setting: at most 60 per hour). Try again later.",
  "retry_after": 3600
}
```

# Field reference

| Field | Description |
|-------|-------------|
| `identifier` | Unique identifier of the rule. Use it to update, remove, or reorder the rule. |
| `position` | Where the rule is checked among the rules managed in DeployHQ, starting at `1`. The `_redirects` file's rules are always checked before all of them. |
| `source` | The path the rule redirects from. |
| `destination` | Where the rule redirects to. |
| `status` | The HTTP status of the redirect: `301`, `302`, `307`, or `308`. |
| `query` | The query parameters the rule matches, such as `p=:id`, or `null` for a rule that matches on the path only. |
| `shadowed_by` | The rule that takes over addresses this rule matches, or `null`: a `_redirects` rule that matches some or all of them (`source` and `line`, with `rule_identifier` set to `null`), or an earlier rule managed in DeployHQ that matches all of them (`source` and `rule_identifier`, with `line` set to `null`). |
| `line` | For a `_redirects` rule or warning, its line number in the file. `null` for a warning about the whole file. |

# Trailing slash setting

The trailing slash setting of a Static Hosting site decides what happens when a visitor asks for a page address with or without a slash at the end. Set it with `trailing_slash` inside the top-level `hosted_website_attributes` of the request that creates or updates the Static Hosting server:

| Value | What it does |
|-------|--------------|
| `strip` | `/about/` redirects to `/about`. The default. |
| `force` | `/about` redirects to `/about/`. Use it for a site moved from WordPress, or any site whose URLs end in a slash. |
| `preserve` | `/about` and `/about/` both work, with no redirect. |

Files with an extension, such as `/logo.png`, are never redirected. For more detail, see [Redirects for Static Hosting](Article: static-hosting-redirects).

## Creating a server

On `POST /projects/:project_id/servers`, pass `trailing_slash` alongside the other site fields:

```bash
curl -X POST \
-H "Content-type: application/json" \
-H "Accept: application/json" \
--user user@example.com:api-key \
-d '{"server":{"name":"marketing-site","protocol_type":"static_hosting"},"hosted_website_attributes":{"subdomain":"marketing-site","spa_mode":false,"trailing_slash":"force"}}' \
https://test.deployhq.com/projects/my-project/servers
```

## Updating a server

On `PUT /projects/:project_id/servers/:server_id`, send `hosted_website_attributes` with `trailing_slash`. You can send it on its own, without a `server` object. A request that does not include `trailing_slash` leaves the setting unchanged. The change is applied to the live site as part of the request, without a deployment, and counts towards the [rate limit](#rate-limit). Sending the value the site already has changes and publishes nothing, and does not count.

```bash
curl -X PUT \
-H "Content-type: application/json" \
-H "Accept: application/json" \
--user user@example.com:api-key \
-d '{"hosted_website_attributes":{"trailing_slash":"force"}}' \
https://test.deployhq.com/projects/my-project/servers/a1b2c3d4-e5f6-7890-abcd-ef1234567890
```

A value other than `strip`, `force`, or `preserve` returns HTTP `422`, and the setting is not changed:

```json
{
  "error": "Trailing slash must be one of strip, force or preserve"
}
```

The setting is applied to the live site as part of the request. If that cannot be done, the trailing slash setting is not changed, but any other fields in the same request are still saved, and the error says so:

| Status | When | `error` |
|--------|------|---------|
| `409` | The site is busy publishing a deployment or another change | `The server was saved, but the trailing slash setting was not changed: the site is busy publishing a deployment or another change. Try again in a moment.` |
| `503` | The change could not be published to the site | `The server was saved, but the trailing slash setting was not changed: it could not be published to the site. Try again in a moment.` |
| `429` | The account is over the [rate limit](#rate-limit) | `The server was saved, but the trailing slash setting was not changed: Too many redirect changes (adding, changing, removing or reordering redirect rules, or changing the trailing slash setting: at most 60 per hour). Try again later.` |

A `429` response also has `retry_after`, in seconds, and a matching `Retry-After` header.

## Reading the setting

The response for a Static Hosting server, when viewing, creating, or updating it, includes a `static_hosting` object with the site's `trailing_slash`:

```json
{
  "static_hosting": {
    "hosted_website_identifier": "e4f5a6b7-c8d9-4e0f-a1b2-c3d4e5f6a7b8",
    "subdomain": "marketing-site",
    "url": "https://marketing-site.deployhq-sites.com",
    "spa_mode": false,
    "trailing_slash": "force",
    "status": "active",
    "active_deployment_uuid": "3c9e1f20-7a4b-4d8e-b5c6-0f1a2b3c4d5e"
  }
}
```

`trailing_slash` is `null` for a site whose setting has never been changed, which behaves as `strip`. Setting it to the behaviour the site already has changes nothing, so setting `strip` on such a site still returns `null`.
