Repository navigation
Cloud: rename redirects page to Routing Rules #814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}'
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
@@ -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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overdue change, thank you! 🫶