Skip to content
Open
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: 2 additions & 0 deletions _redirects
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
/docs/5.x/customers.html /docs/5.x/system/customers.html
/docs/6.x/extend/introduction.html /docs/6.x/extend/index.html

/docs/cloud/redirects.html /docs/cloud/routing.html

/docs/commerce/3.x/adding-to-and-updating-the-cart.html /docs/commerce/3.x/orders-carts.html#adding-items-to-a-cart
/docs/commerce/3.x/adjusters.html /docs/commerce/3.x/extend/adjusters.html
/docs/commerce/3.x/cart.html /docs/commerce/3.x/orders-carts.html#carts
Expand Down
2 changes: 1 addition & 1 deletion docs/.vuepress/sets/craft-cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = {
"backups",
"regions",
"domains",
"redirects",
"routing",
"plugin-development",
"status",
"cloudflare",
Expand Down
2 changes: 1 addition & 1 deletion docs/cloud/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ The Cloud extension will construct predictable production URLs for _existing_ as

## Custom URLs

You can configure a custom [rewrite](redirects.md#rewrites) to serve assets from additional, non-default URLs or paths.
You can configure a custom [rewrite](routing.md#rewrites) to serve assets from additional, non-default URLs or paths.
However, Craft will continue to generate canonical CDN URLs, unless you explicitly set a **Base URL** in the filesystem’s settings.

::: danger
Expand Down
2 changes: 1 addition & 1 deletion docs/cloud/builds.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ The `current` segment takes the place of a specific build ID, and will always po

### Rewriting Artifact URLs

As an alternative to replacing root-relative asset references in your templates, you can create [rewrite rules](redirects.md) to proxy individual files (or entire directories) from the CDN:
As an alternative to replacing root-relative asset references in your templates, you can create [rewrite rules](routing.md) to proxy individual files (or entire directories) from the CDN:

```yml
rewrites:
Expand Down
2 changes: 1 addition & 1 deletion docs/cloud/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ For the latest information on supported PHP and Node.js versions, see our [Cloud

### Redirects and Rewrites

In addition to the above runtime and build settings, you can configure any number of [redirection and rewrite rules](redirects.md). These rules are evaluated _before_ the request reaches your Craft application, and can be used to normalize URLs, proxy assets or artifacts, redirect secondary domains, or map legacy URLs to your new site.
In addition to the above runtime and build settings, you can configure any number of [redirection and rewrite rules](routing.md). These rules are evaluated _before_ the request reaches your Craft application, and can be used to normalize URLs, proxy assets or artifacts, redirect secondary domains, or map legacy URLs to your new site.

### Cache Keys

Expand Down
4 changes: 2 additions & 2 deletions docs/cloud/domains.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ We have _not_ been able to verify CNAME flattening or ANAME support for these se
Each domain can have any number of subdomains.
Subdomains can point to the same environment as the primary domain (great for multi-site projects) or a different environment (perfect for a staging environment).

You may also directly add and verify a non-apex domain if your project will never need to serve traffic from the apex—but we only recommend this in limited circumstances, like when using [rewrites and redirects](redirects.md).
You may also directly add and verify a non-apex domain if your project will never need to serve traffic from the apex—but we only recommend this in limited circumstances, like when using [rewrites and redirects](routing.md).
Domain [verification](#verify-ownership) is possible without routing traffic to a apex domain; you can then send traffic from more subdomains using the `CNAME` records in the **Route Traffic** table.

## SSL
Expand All @@ -123,7 +123,7 @@ Every Cloud project includes one root domain, and as many subdomains as you need

## Redirection

After adding your apex domain _and_ a `www` subdomain, you can create a [redirection rule](redirects.md) that matches one or the other to ensure traffic is always served at a single address.
After adding your apex domain _and_ a `www` subdomain, you can create a [redirection rule](routing.md) that matches one or the other to ensure traffic is always served at a single address.

## Troubleshooting

Expand Down
2 changes: 1 addition & 1 deletion docs/cloud/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Cloudflare refers to this as the “[Orange-to-Orange](https://developers.cloudf
Nope, sorry!
Craft Cloud is serverless and doesn’t use a traditional HTTP server like nginx or Apache.

However, [redirects and rewrites](redirects.md) can be configured via your `craft-cloud.yaml` file.
However, [redirects and rewrites](routing.md) can be configured via your `craft-cloud.yaml` file.

<a id="ssh"></a>

Expand Down
51 changes: 41 additions & 10 deletions docs/cloud/redirects.md → docs/cloud/routing.md

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overdue change, thank you! 🫶

Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
# Redirects and Rewrites
# Routing Rules

Craft Cloud allows you to define any number of custom [redirect](#redirects) and [rewrite](#rewrites) rules via your [`craft-cloud.yaml` config file](config.md).

Both features use the flexible [`URLPattern` API](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern/exec) to match request URLs. These rules are evaluated on our gateway, _before_ we pass the request to your Craft application.

## Redirects

Redirection rules live in your `craft-cloud.yaml` file, under a `redirects` key:

```yml
php-version: '8.5'

# ...

redirects:
- pattern:
hostname: 'legacy-domain.com'
destination: 'https://new-domain.com/{request.uri}'
hostname: 'promo-domain.com'
destination: 'https://main-domain.com/promo'
status: 301

rewrites:
- pattern:
hostname: 'delivery.main-domain.com'
destination: '{assetBaseUrl}/{request.uri}'
```

## Patterns and Destinations

Every rule has a `pattern` that decides which requests it matches, and a `destination` that builds the URL to send them to.

### Patterns

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to preserve a basic example before getting into the structure. We get into the weeds really fast. 😬

Assembled from later examples…

php-version: '8.5'

# ...

redirects:
  - pattern:
      hostname: 'promo-domain.com'
    destination: 'https://main-domain.com/promo'
    status: 301

rewrites:
  - pattern:
      hostname: 'delivery.main-domain.com'
    destination: '{assetBaseUrl}/{request.uri}'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added your example as-is, right after the intro and before "Patterns and Destinations" (6035e8f).


Each rule’s `pattern` can be a string or an object; either way, it is passed verbatim to the [`new URLPattern(input)` constructor](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern/URLPattern). In effect, you can match based on [parts of a URL](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern/URLPattern#input), without using complex regular expressions.

A pattern (or each of its components) can be a simple string, or include [wildcards and capture groups](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API#concepts_and_usage).

### Destinations

The `destination` is a pseudo-template used to build a complete URL. It can be a static string or contain substitute information about the original request (`request`) or pattern (`matches`). Additionally, these variables are available to simplify constructing URLs to other Craft Cloud resources:

- `cdnBaseUrl` — The Craft Cloud CDN domain, plus the requested environment’s identifier.
Expand All @@ -42,6 +51,28 @@ The `request` object contains these properties:
- `method` — The request’s [HTTP verb](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods).
- `headers` — A map of request header names (case-insensitive) and values.

Rewrite rules’ [request header templates](#headers) use the same syntax, with a few extra variables.

Put keys that contain dots in brackets. For example, a header template reads the `app.id` cookie with `{request.cookies["app.id"]}`.

To output a literal brace, escape it as `\{` or `\}`. In double-quoted YAML strings, write `\\{` and `\\}` instead.

## Redirects

Redirection rules live in your `craft-cloud.yaml` file, under a `redirects` key:

```yml
php-version: '8.5'

# ...

redirects:
- pattern:
hostname: 'legacy-domain.com'
destination: 'https://new-domain.com/{request.uri}'
status: 301
```

### HTTP Status Codes

An optional `status` key in each rule determines what [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status) is sent with the redirection. If none is specified, we default to [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/302) for a “temporary” redirection.
Expand Down Expand Up @@ -99,7 +130,7 @@ redirects:

Unlike a [redirect](#redirects), URL rewrites are used to create virtual resources at our gateway—effectively giving you a configurable proxy. This means that when a matching URL is requested, the corresponding origin’s response is sent back, verbatim, without an initial 300-level response.

Rules follow essentially the same structure as redirects: the pattern-matching process and destination templates are identical. Rules are created under a `rewrites` key in your `craft-cloud.yaml` config file.
Rules use the same [patterns and destinations](#patterns-and-destinations) as redirects, and are created under a `rewrites` key in your `craft-cloud.yaml` config file.

::: tip
Using rewrites, you can avoid referencing `cdn.craft.cloud` or updating templates to use [`cloud.artifactUrl`](builds.md#artifact-urls)–any CDN requests can be served directly from your custom domain.
Expand Down Expand Up @@ -187,9 +218,9 @@ The `request` options only apply to these destinations. The gateway doesn’t ca

#### Headers

`request.headers` maps header names to values. Values are parsed as templates, using the same placeholders as `destination`, plus two objects that only header templates can read:
`request.headers` maps header names to values. Values are parsed as templates, using the same placeholders as a [`destination`](#destinations), plus two objects that only header templates can read:

- `request.cookies`: The visitor’s cookies, by name, with the raw values the browser sent. Put names that contain dots in brackets: `{request.cookies["app.id"]}`.
- `request.cookies`: The visitor’s cookies, by name, with the raw values the browser sent. Put names that contain dots in brackets, like `{request.cookies["app.id"]}`.
- `request.cf`: [Request data from Cloudflare](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), such as `country`, `regionCode`, and `city`.

```yml
Expand Down
2 changes: 1 addition & 1 deletion docs/cloud/static-caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Cloud uses the first rule that matches, so you should declare them with _descend
Note that customizing `query-string` _replaces_ our default behavior and [exclusion list](#ignored-params).
:::

Each `pattern` uses the same URL matching logic as [rewrites and redirects](redirects.md), and must define at least a `query-string` or `cookies` key.
Each `pattern` uses the same URL matching logic as [rewrites and redirects](routing.md), and must define at least a `query-string` or `cookies` key.

- **Query String** (`query-string`) — Two nested keys are required:
- `mode` — Determines the behavior for this rule. `include` treats the provided `keys` as an allow-list; `exclude` treats it as a deny-list.
Expand Down
Loading