diff --git a/_redirects b/_redirects index c22dc7822..948076e74 100644 --- a/_redirects +++ b/_redirects @@ -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 diff --git a/docs/.vuepress/sets/craft-cloud.js b/docs/.vuepress/sets/craft-cloud.js index 38ab08f30..259091c81 100644 --- a/docs/.vuepress/sets/craft-cloud.js +++ b/docs/.vuepress/sets/craft-cloud.js @@ -60,7 +60,7 @@ module.exports = { "backups", "regions", "domains", - "redirects", + "routing", "plugin-development", "status", "cloudflare", diff --git a/docs/cloud/assets.md b/docs/cloud/assets.md index b2868ff55..dbfabda39 100644 --- a/docs/cloud/assets.md +++ b/docs/cloud/assets.md @@ -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 diff --git a/docs/cloud/builds.md b/docs/cloud/builds.md index f6c099e52..7bee7648c 100644 --- a/docs/cloud/builds.md +++ b/docs/cloud/builds.md @@ -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: diff --git a/docs/cloud/config.md b/docs/cloud/config.md index 7d1b12c0c..c2d8ef8c1 100644 --- a/docs/cloud/config.md +++ b/docs/cloud/config.md @@ -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 diff --git a/docs/cloud/domains.md b/docs/cloud/domains.md index 1916fee08..df61b0fde 100644 --- a/docs/cloud/domains.md +++ b/docs/cloud/domains.md @@ -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 @@ -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 diff --git a/docs/cloud/faq.md b/docs/cloud/faq.md index 36e89906f..e4082bc56 100644 --- a/docs/cloud/faq.md +++ b/docs/cloud/faq.md @@ -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. diff --git a/docs/cloud/redirects.md b/docs/cloud/routing.md similarity index 90% rename from docs/cloud/redirects.md rename to docs/cloud/routing.md index 9320db423..a3971c4c3 100644 --- a/docs/cloud/redirects.md +++ b/docs/cloud/routing.md @@ -1,13 +1,9 @@ -# 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' @@ -15,15 +11,28 @@ 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 + 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. @@ -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. @@ -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. @@ -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 diff --git a/docs/cloud/static-caching.md b/docs/cloud/static-caching.md index 817f47e43..2215781f4 100644 --- a/docs/cloud/static-caching.md +++ b/docs/cloud/static-caching.md @@ -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.