> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sevalla.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Static Sites - Redirects

> Find out how to add redirects to your static sites in Sevalla.

If a static site has a `_redirects` file in the repository's root directory, Sevalla will parse the file's contents and apply the custom redirect rules.

## Redirect rule format and examples

Each redirect rule must be placed on its own line, with the original path followed by the new path or destination URL. Any line beginning with `#` will be treated as a comment and ignored. Paths are case-sensitive, and special characters must be URL-encoded.

For example:

```text theme={null}
# Redirects
/shop                 /store
/products/item.php    /products/item
/events               /community/events
/old-dashboard        https://example.com/new-dashboard
/team/john%smith%1    /team/about-john%smith%1
```

You can further customize redirect behavior by adding options at the end of each line, such as HTTP status codes, country rules, or language conditions.

### HTTP status code example

Redirect permanently using a `301` status code:

```text theme={null}
/old-page   /new-page   301
```

Temporary redirect using a `302` status code:

```text theme={null}
/sale   /summer-sale   302
```

### Country rule example

Redirect visitors from France to a localized page:

```text theme={null}
/pricing   /fr/pricing   302   Country=fr
```

Redirect visitors from the United States:

```text theme={null}
/store   /us/store   302   Country=us
```

### Language condition example

Redirect users who prefer Spanish based on the `Accept-Language` header:

```text theme={null}
/docs   /es/docs   302   Language=es
```

Redirect users requesting German content:

```text theme={null}
/help   /de/help   302   Language=de
```

Redirect rules are processed from top to bottom, and the first matching rule encountered is applied. If multiple rules target the same path, only the first one will be used, and all subsequent rules for that path will be ignored.

## Header-based conditions

You can apply redirect rules when a request only contains a specific HTTP header by adding a `Header:` condition at the end of the rule.

For example, the following rule redirects requests to the Markdown version of a page only when the client requests the `text/markdown` content type:

```text theme={null}
/quickstart/   /quickstart.md   301   Header:Accept=text/markdown
```

Header names are matched case-insensitively.

This is useful for:

* content negotiation,
* API or client-specific redirects,
* serving alternate formats,
* or applying redirects only for certain request types.

## Custom 404 page handling

You can create a custom 404 page for any path that doesn’t resolve to a static file; no redirect rules are required. Simply add a `404.html` file to your site, and it will automatically be shown whenever a path cannot be found.

You can also combine custom 404 pages with redirects by defining explicit rules for different languages or directory paths. For example:

```text theme={null}
/en/* /en/404.html 404
/de/* /de/404.html 404
```

These rules ensure that the corresponding 404 pages are displayed only for missing assets within those specific paths.

## Force redirects

In some cases, a redirect may not occur because an existing file matches the requested URL path. To override this behavior, you can force the redirect by adding an exclamation mark (`!`) to the status code.

For example:

```text theme={null}
/store/offers  /store/best-offers.html 200!
```

In this example, `/store/offers` will always serve the content from `/store/best-offers.html`, even if a file such as `/store/offers/index.html` exists.

## Splats

An asterisk (`*`) represents a splat, which matches anything that follows it in a path, for example:

```text theme={null}
/resources/*   /library/:splat
```

This would redirect a URL such as `/resources/guides/getting-started`\
to `/library/guides/getting-started`.

Redirect rules always apply the first matching rule, therefore, more specific rules should appear before more general ones.

The following limitations apply to splats:

* \*\*No mid-path wildcards: \*\*You cannot place an asterisk in the middle of a path—for example, `/docs/*.html` is not valid. Splats can only appear at the end of a segment.
* \*\*No exclusions within a splat rule: \*\*You cannot exclude specific paths directly within a splat redirect. To handle exceptions, create a more specific rule above the splat rule, ensuring it is matched first.

## Placeholders

You can use placeholders in both the origin and target paths to dynamically capture and reuse parts of a URL, for example:

```text theme={null}
/store/:category/:item   /products/:category/:item
```

In this example, a URL such as`/store/electronics/headphones`would redirect to `/products/electronics/headphones`.

A placeholder matches either:

* a single path segment between two slashes (`/`), or
* the final segment of a path, including any file extension but excluding the query string.

## Query parameters

You can use query parameters to create more precise redirect rules.

### Matching a single query parameter

**Example:**

```text theme={null}
/search q=:term   /results/:term   301
```

This redirects a URL like `/search?q=laptops` to `/results/laptops` with a 301 redirect.

This rule matches only when the `q` parameter is present and no additional parameters are included. If the URL includes other parameters, such as `&sort=asc`, it will not match this rule.

### Matching multiple query parameters

Add each parameter as its own key/value pair, separated by a space:

```text theme={null}
/products category=:category brand=:brand   /browse/:category/:brand   301
```

This matches URLs such as `/products?category=shoes&brand=nike` to `/browse/shoes/nike`

### Handling optional or multiple parameter combinations

When parameters may or may not be present, list redirects from most specific to most general so the correct rule is matched first.

**Examples:**

```text theme={null}
# Both parameters present (order from the browser does not matter)
/items/* color=:color size=:size   /catalog/:color/:size/:splat   301

# Only one parameter present (must match exactly)
/items/* color=:color              /catalog/:color/:splat         301
/items/* size=:size                /catalog/:size/:splat          301

# Base rule when no parameters match
# If this is the only rule, all query parameters are forwarded automatically.
/items/*                           /catalog/:splat                301
```

This pattern ensures all variations, both parameters, one parameter, or none, are handled correctly.

## Single page applications (SPAs)

If your site is a single-page application, we strongly recommend adding a `_redirects` file to the root of your repository with the following rule:

```text theme={null}
/*   /index.html   200
```

This ensures that all routes, including deep links and client-side navigation, serve your `index.html` file with a 200 status, allowing your SPA’s router to handle the actual path logic.
