Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/v3/configuration/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ maintenance-mode-status = 503
# [[advanced.rewrites]]
# source = "**/*.{png,ico,gif}"
# destination = "/assets/favicon.ico"
## Optional redirection
# redirect = 301

# [[advanced.rewrites]]
# source = "**/*.{jpg,jpeg}"
Expand Down
12 changes: 8 additions & 4 deletions src/v3/features/custom-http-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ A particular set of HTTP headers can only be applied when a `source` matches aga

### Source

The source is a [Glob pattern](<https://en.wikipedia.org/wiki/Glob_(programming)>) that should match against the URI that is requesting a resource file.
The source is a [Glob pattern](<https://en.wikipedia.org/wiki/Glob_(programming)>) that should match against the URI that is requesting a resource file. Unlike in [URL redirects](url-redirects.md), `*` also matches `/` here.

An entry applies when its source matches the request path or the path of the file that answers the request. So `/dir/` gets the headers of `/dir/index.html` as well as those of `/dir/`, `/page` those of `/page.html`, and a [negotiated markdown](markdown-content-negotiation.md) variant those of its `.md` file. The `308` that adds a [trailing slash](trailing-slash-redirect.md) serves no file.

Both paths are matched as SWS serves them, decoded and without empty or `.` segments, so `//a/./b` and `/a%2Fb` get the headers of `source = "/a/b"`. See [path matching](url-redirects.md#path-matching) for how to write spaces, non-ASCII and glob characters in a source.

### Headers

A set of valid plain [HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) to be applied.
A set of valid plain [HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) to be applied. An array sends the header once per value, for example `Link = ["</a.css>; rel=preload", "</b.js>; rel=preload"]`.

### Status

Expand All @@ -52,9 +56,9 @@ SWS validates the codes at startup and fails to start on an empty array or a cod
An optional string that selects which responses the entry applies to:

- `files` (default): responses of the static file handler only, including error responses such as `404 Not Found` and plain [URL rewrites](url-rewrites.md), which are served as files.
- `all`: also the responses SWS returns before serving a file: [URL redirects](url-redirects.md), [URL rewrites](url-rewrites.md) with `redirect`, the [health endpoints](health-endpoint.md), the [metrics endpoint](metrics.md), CORS requests rejected with `403 Forbidden`, [Basic Authentication](basic-authentication.md) challenges, [maintenance mode](maintenance-mode.md) and `405 Method Not Allowed`.
- `all`: also the responses SWS returns before serving a file: [URL redirects](url-redirects.md), the [health endpoints](health-endpoint.md), the [metrics endpoint](metrics.md), CORS requests rejected with `403 Forbidden`, [Basic Authentication](basic-authentication.md) challenges, [maintenance mode](maintenance-mode.md) and `405 Method Not Allowed`.

These early responses serve no file, so `source` is matched against the request path as is. `scope` can be combined with `status`.
These early responses serve no file, so `source` is matched against the request path only. `scope` can be combined with `status`.

> [!WARNING] Broad patterns
>
Expand Down
4 changes: 4 additions & 0 deletions src/v3/features/trailing-slash-redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ static-web-server \
--root ./my-public-dir \
--redirect-trailing-slash true
```

When enabled, a request for a directory without a trailing slash, such as `/docs`, gets a `308 Permanent Redirect` to `/docs/`. This also applies to requests [rewritten](url-rewrites.md#trailing-slash-redirect) to a directory.

A regular file requested with a trailing slash, such as `/app.js/`, answers `404 Not Found`.
59 changes: 48 additions & 11 deletions src/v3/features/url-redirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,27 @@ Each table entry should have the following key/value pairs:
>
> The incoming request(s) will reach the `destination` only if the request(s) URI matches the `source` pattern.

Rules apply in order and the first match wins. Redirects run before [URL rewrites](url-rewrites.md).

### Host

Optional `host` redirect entry to be matched against the incoming host URI. If a `host` redirect setting is specified then SWS will attempt to match the value against the incoming URI host (request), applying the required redirect entry or ignoring it otherwise.

- The value is a host name, an IPv4 address or a bracketed IPv6 address (`[::1]`), with an optional port.
- Letter case is ignored and IPv6 addresses are compared by value.
- Without a port, the entry matches any port. With a port, such as `example.com:8080`, it matches only requests carrying that port. A default port is not implied: `example.com:80` never matches `Host: example.com`.
- The host is taken from the request URI (absolute-form requests, HTTP/2) or else from the `Host` header.

SWS fails to start on an invalid `host`, including wildcards such as `*.example.com`.

> [!TIP] www to non-www redirects
>
> The host entry allows for instance to perform www to non-www redirects or vice versa (see example below).

> [!WARNING] The client chooses the host
>
> A host-bound rule only applies to requests sent with that host. The same path requested with another host skips it, so don't rely on such a rule to protect content served for several hosts.

### Source

The source is a [Glob pattern](<https://en.wikipedia.org/wiki/Glob_(programming)>) that should match against the URI that is requesting a resource file.
Expand All @@ -48,16 +61,40 @@ The glob pattern functionality is powered by the [globset](https://docs.rs/globs
>
> In later versions, the default has changed such that `*` does not match the path separator.

#### Path matching

Sources match the request path as SWS serves it: decoded, without empty or `.` segments. So `//a/./b`, `/a%2Fb` and `/%61/b` all match `source = "/a/b"`. The query string is not part of the path. On macOS and Windows, ASCII letter case is ignored, as their file systems do.

Write a source as the path is served:

- A source that starts with neither `/` nor a glob character, or holds `//`, a `.` or `..` segment or an escaped `/` (`%2F`), matches no request. SWS logs a warning at startup.
- A space, a non-ASCII character or a percent-escape in either hex case can be written as is: `/my files/*`, `/café/*` and `/caf%C3%A9/*` are the same source.
- To match a glob character such as `[`, `{` or `?` literally, quote it with a backslash (`/a\[b`) or as the only member of a class (`/a[[]b`). On Windows, `\` separates path segments, so use the class.
- `?` and a class such as `[a-z]` match one character of the percent-encoded path, so never a space or another character the path holds encoded. SWS fails to start on a class that holds such a character among other members, in a range or negated (`/[a b]x`), and suggests an alternation instead (`{a,%20}`).

Some URLs serve the same content and are still separate paths, each needing its own rule: a directory and its index file (`/dir/` and `/dir/index.html`), a path and its `.html` file (`/page` and `/page.html`), and a file and its pre-compressed `.gz`, `.br` or `.zst` variant requested by name.

When rules are configured, a request path longer than 8 KiB answers `414 URI Too Long`.

### Destination

The value can be either a local file path that maps to an existing file on the system or an external URL.
It could look like `/some/directory/file.html`. It is worth noting that the `/` at the beginning indicates the server's root directory.

The request query string is appended to the destination, after the destination's own query and before its fragment. For example, `destination = "/new?from=old"` redirects `/old?page=2` to `/new?from=old&page=2`.

SWS fails to start when a destination holds a character that a URI only holds percent-encoded, such as a space, `"`, `<`, `>`, `\`, `|` or a non-ASCII character. Write it percent-encoded instead (`%20` for a space).

#### Replacements

Additionally, a `destination` supports replacements for every Glob pattern group that matches against the `source`.
The replacement order starts from `0` to `n` and is defined with a dollar sign followed by an index (Glob pattern group occurrence).

- `$0` is the whole path. Every `{a,b}` alternation, `{*}` or `{**}` group and `**/` segment is a group from `$1` on, in source order. In `**/{*}.{jpg,png}`, `$1` is the leading directory, `$2` the file name and `$3` the extension. A trailing `/**` is not a group: use `/{**}` to capture it.
- Captures take the served spelling of the path: `/old/a%2fb` with `destination = "/new/$1"` gives `/new/a/b`.
- Placeholders are only allowed in the path, query or fragment of a destination: `https://$1.example.org/` fails at startup. A destination that starts with a placeholder, such as `/$1`, stays a path on the same host.
- A placeholder in the query is percent-encoded as one value, so its `&`, `=`, `+` and `;` can't add parameters.

> [!TIP] Group your Glob patterns
>
> When using replacements, also group your Glob pattern by surrounding them with curly braces so every group should map to its corresponding replacement.<br>
Expand Down Expand Up @@ -95,8 +132,9 @@ kind = 302
## Note that we're using curly braces to group the `*` wildcard.
## See https://docs.rs/globset/latest/globset/#syntax
source = "**/{*}.{jpg,jpeg,svg}"
## `$1` is the leading directory, `$2` the file name and `$3` the extension.
## For example, the destination will result in `http://localhost/assets/abcdef.jpeg`
destination = "http://localhost/assets/$1.$2"
destination = "http://localhost/assets/$2.$3"
kind = 301

# d. Simple route redirect using the `host` option
Expand All @@ -111,18 +149,17 @@ kind = 301
If you request something like:

```sh
curl -I http://localhost:4433/abcdef.jpeg
curl -I http://localhost:8080/abcdef.jpeg
```

Then the server logs should look something like this:
Then the server logs (`--log-level debug --log-format pretty`) should look something like this:

```log
2023-07-11T21:11:22.217358Z INFO static_web_server::handler: incoming request: method=HEAD uri=/abcdef.jpeg
2023-07-11T21:11:22.217974Z DEBUG static_web_server::handler: url redirects glob pattern: ["$0", "$1", "$2"]
2023-07-11T21:11:22.217992Z DEBUG static_web_server::handler: url redirects regex equivalent: (?-u)^(?:/?|.*/)(?:[^/]*)\.(?:svg|jpeg|jpg)$
2023-07-11T21:11:22.218002Z DEBUG static_web_server::handler: url redirects glob pattern captures: ["abcdef.jpeg", "abcdef", "jpeg"]
2023-07-11T21:11:22.218076Z DEBUG static_web_server::handler: url redirects glob pattern destination: "http://localhost/assets/$1.$2"
2023-07-11T21:11:22.218712Z DEBUG static_web_server::handler: url redirects glob pattern destination replaced: "http://localhost/assets/abcdef.jpeg"
2023-07-11T21:11:22.218739Z TRACE static_web_server::handler: uri matches redirects glob pattern, redirecting with status '301 Moved Permanently'
...
2026-11-28T21:47:33.778285+01:00 DEBUG static_web_server::url_rules::destination: url rule regex equivalent: (?i)^(/?|.*/)([^/]*)\.(jpg|jpeg|svg)$
2026-11-28T21:47:33.778339+01:00 DEBUG static_web_server::url_rules::destination: url rule glob pattern captures: ["/abcdef.jpeg", "/", "abcdef", "jpeg"]
2026-11-28T21:47:33.778355+01:00 DEBUG static_web_server::url_rules::destination: url rule glob pattern destination: "http://localhost/assets/$2.$3"
2026-11-28T21:47:33.778379+01:00 DEBUG static_web_server::url_rules::destination: url rule glob pattern destination replaced: "http://localhost/assets/abcdef.jpeg"
2026-11-28T21:47:33.778414+01:00 INFO static_web_server::log_addr: response method="HEAD" uri="/abcdef.jpeg" status=301
```

The `(?i)` prefix appears on macOS and Windows only, where letter case is ignored.
Loading