Route limits
Please note that the maximum size of the routes document is 128 KB, which should fit around 300 different routes. Read more about route limits here.Whole-route redirects
Using whole-route redirects, you can define very basic routes in your.upsun/config.yaml file whose sole purpose is to redirect. A typical use case for this type of route is adding or removing a www. prefix to your domain, as the following example shows:
.upsun/config.yaml
301. See Specify a HTTP status code to change the status code that is used.
Partial redirects
In the.upsun/config.yaml file you can also add partial redirect rules to existing routes:
.upsun/config.yaml
302. See Specify a HTTP status code to change the status code that is used.
Two keys are available under redirects:
| Key | Required | Description |
|---|---|---|
expires | No | The duration the redirect is cached. Examples of valid values include 3600s, 1d, 2w, 3m.To disable caching for all your redirects, set expires to 0. You can also disable caching on a specific redirect. |
paths | Yes | The paths to be redirected |
paths is defined by a key describing:
- The expression to match against the request path
- A value object describing both the destination to redirect to, with detail on how to handle the redirection
| Key | Required | Default | Description |
|---|---|---|---|
to | Yes | n/a | A relative URL - '/destination', or absolute URL - 'https://example.com/'. |
regexp | No | false | Specifies whether the path key should be interpreted as a PCRE regular expression. If you use a capturing group, the replace field ($1) has to come after a slash (/). More information. |
prefix | No | true, but not supported if regexp is true | Specifies whether both the path and all its children or just the path itself should be redirected. More information. |
append_suffix | No | true, but not supported if regexp is true or if prefix is false | Determines if the suffix is carried over with the redirect. More information. |
code | No | n/a | HTTP status code. Valid status codes are 301, 302, 307, and 308. Defaults to 302 for Partial redirects, and 301 for Whole-route redirects. More information. |
expires | No | Defaults to the expires value defined directly under the redirects key, but can be fine-tuned. To disable caching on a specific redirect, set expires to 0. | The duration the redirect is cached for. More information. |
regexp).Alternatively, and in many cases, you can use the
prefix and/or append_suffix keys to achieve the same results.Here are some examples to illustrate this and help you choose a method for your partial redirects:
- `regexp`
- `prefix`
- `append_suffix`
Consider this It achieves the same result as this basic redirect:
regexp redirect:Redirects using regular expressions
You can use regular expressions to configure your redirects. In the following example, a request tohttps://example.com/foo/a/b/c/bar redirects to https://example.com/a/b/c:
.upsun/config.yaml
to statement are available when regexp is set to true:
$is_argsevaluates to?or empty string$argsevaluates to the full query string if any$arg_fooevaluates to the value of the query parameterfoo$urievaluates to the full URI of the request.
Redirects using prefix and append_suffix
Instead of using regular expressions to configure your redirects,
you might want to use the prefix and append_suffix keys.
When set to true, which is their default value, prefix and append_suffix are equivalent.
For example:
.upsun/config.yaml
/fromredirects to/to/from/some/pathredirects to/to/some/path
false, prefix and append_suffix behave differently.
For example, with the following configuration:
/from/ redirects to /to/some/path, but a request to /from/some/path does not.
And with the following configuration:
/from/some/path (and any path after /from) redirects to just /to.
Specify a HTTP status code
To set a specific HTTP status code for your redirect, use thecode key:
/from use a 308 HTTP status code,
while redirects from /here default to 302 (“found”, as long as the resource is indeed found as a result from the redirect).
Manage caching
You can set an expiration time on your redirects or even disable caching on them.Set an expiration time on your redirects
You can specify how long you want your redirects to be cached for. To do so, use theexpires key under the redirects key.
In the following example, all redirects are cached for two weeks:
expires key, but under the paths key.
In the following example:
- The first redirect uses the default expiration time set on all redirects and is cached for two weeks
- The second redirect ignores the default expiration time set on all redirects, and is cached for three days instead
You can set an expiration time on a specific redirect (under the
paths key)
even if you haven’t defined a default expiration time on all your redirects (under the redirects key).The expiration time you set on a specific redirect (under the paths key) overwrites any default expiration time
you may have set on all your redirects (under the redirects key).Disable caching on your redirects
To disable caching on all your redirects, set theexpires key to 0 under the redirects key:
expires key to 0 under the relevant path in the paths section.
In the following example, caching is disabled on the second redirect only: