Skip to content

Commit fb28776

Browse files
committed
Routing + redirection clarifications
1 parent 81ffd50 commit fb28776

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

docs/5.x/configure.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ The most common way to customize your Craft project is by editing files in the [
3333
| [General Configuration](#general) | `general.php` | Global options that can affect the front-end, control panel, debugging, etc.
3434
| [Database Settings](#database) | `db.php` | Connection settings for your database.
3535
| [Custom Options](#custom-settings) | `custom.php` | Arbitrary key-value storage for your own options.
36-
| [Routing](#url-rules) | `routes.php`, `redirects.php` | Custom HTTP routes and redirect rules.
36+
| [Routing](#url-rules) | `routes.php` | Custom HTTP routes.
37+
| [Redirection](system/routing.md#redirection) | `redirects.php` | Additional patterns for redirection.
3738
| [Application Configuration](#application-configuration) | `app.php`, `app.web.php`, `app.console.php` | Overrides for the root application and any of its [Components](https://www.yiiframework.com/doc/guide/2.0/en/concept-components).
3839
| Plugin Settings | `{plugin-handle}.php`, or other custom files | Consult the plugin’s documentation for specifics.
3940
| [Advanced](#advanced) | | Specific library options and/or behaviors that may be exposed in a non-standard way.
@@ -483,7 +484,7 @@ Your [database connection settings](reference/config/db.md) are set via the `con
483484

484485
You can define custom [URL rules](guide:runtime-routing#url-rules) in `config/routes.php`, and [redirection rules](system/routing.md#redirection) in `config/redirects.php`. <Since ver="5.6.0" feature="Redirection rules" />
485486

486-
See [Routing](system/routing.md) for more details.
487+
See [Routing](system/routing.md) for more details about Craft’s request handling behavior.
487488

488489
### Application Configuration
489490

docs/5.x/system/routing.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ return [
299299
];
300300
```
301301

302+
::: tip
303+
To send a user to the homepage, use an empty string (`''`) for the `to` value.
304+
:::
305+
302306
Rules can contain [parameters](#advanced-routing-with-url-rules), as well:
303307

304308
```php
@@ -315,6 +319,12 @@ If you need complete control over the matching _and_ redirection logic, create a
315319
return [
316320
[
317321
'match' => function (\Psr\Http\Message\UriInterface $url): ?string {
322+
// Match a path:
323+
if ($url->getPath() !== 'customer_order.aspx') {
324+
return null;
325+
}
326+
327+
// Or test for the presence of a query parameter:
318328
parse_str($url->getQuery(), $params);
319329

320330
if (!isset($params['orderId'])) {
@@ -328,4 +338,26 @@ return [
328338
];
329339
```
330340

331-
You may return `null` from a match callback to skip the rule and continue processing.
341+
You may return `null` from a match callback to skip the rule and continue processing. Callbacks are only invoked if all previous rules fail to match.
342+
343+
#### Multi-Site Redirection
344+
345+
Unlike routes, redirection rules are processed globally, and matched against full paths. For example, a rule like `old/page` would _not_ catch a request path beginning with a site’s base path, like `/secondary-site/old/page`. You may parameterize rules to match multiple sites’ base paths (i.e: `<site:{slug}>/old/page`), or define individual rules for each relevant site.
346+
347+
However, as part of Craft’s normal routing behavior, every front-end request is handled in the context of a site—typically one with the correct hostname and the longest matching base path, or the “default” site when none match. When a redirect rule is evaluated, its `to` value is treated as site-relative, meaning this rule…
348+
349+
```php
350+
return [
351+
'corporate/team' => 'about-us',
352+
];
353+
```
354+
355+
…behaves differently depending on whether or not a site exists with the base path `corporate`. _Without_, it would redirect normally (`/corporate/team` to `/about-us`), but _with_, requests to `/corporate/team` would actually end up at `/corporate/about`, because Craft tried to route the request in the “Corporate” site context.
356+
357+
To avoid this ambiguity, prefix the destination with a slash (`/`) to make it an “absolute” path:
358+
359+
```php
360+
return [
361+
'corporate/team' => '/about-us',
362+
];
363+
```

0 commit comments

Comments
 (0)