You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| [Redirection](system/routing.md#redirection) | `redirects.php` | Additional patterns for redirection.
37
38
| [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).
38
39
| Plugin Settings | `{plugin-handle}.php`, or other custom files | Consult the plugin’s documentation for specifics.
39
40
| [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
483
484
484
485
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`. <Sincever="5.6.0"feature="Redirection rules" />
485
486
486
-
See [Routing](system/routing.md) for more details.
487
+
See [Routing](system/routing.md) for more details about Craft’s request handling behavior.
Copy file name to clipboardExpand all lines: docs/5.x/system/routing.md
+33-1Lines changed: 33 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -299,6 +299,10 @@ return [
299
299
];
300
300
```
301
301
302
+
::: tip
303
+
To send a user to the homepage, use an empty string (`''`) for the `to` value.
304
+
:::
305
+
302
306
Rules can contain [parameters](#advanced-routing-with-url-rules), as well:
303
307
304
308
```php
@@ -315,6 +319,12 @@ If you need complete control over the matching _and_ redirection logic, create a
315
319
return [
316
320
[
317
321
'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:
318
328
parse_str($url->getQuery(), $params);
319
329
320
330
if (!isset($params['orderId'])) {
@@ -328,4 +338,26 @@ return [
328
338
];
329
339
```
330
340
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:
0 commit comments