Redirects
Managing redirection rules is a common requirement for web applications, especially in cases where you do not want to lose incoming links that have changed or been removed over time. The following demonstrates how to manage redirection rules on your Magento Commerce Cloud projects.
If the redirection methods discussed in this topic do not work for you, you can use caching headers to do the same thing.
Whole-route redirects
Using whole-route redirects, you can define very basic routes using the routes.yaml
file. For example, you can redirect from a naked domain to a www
subdomain as follows:
1
2
3
http://{default}/:
type: redirect
to: http://www.{default}/
Partial redirects
In the .magento/routes.yaml
file you can also add partial redirect rules
to existing routes:
1
2
3
4
5
6
http://{default}/:
redirects:
expires: 1d
paths:
"/from": { "to": "http://example.com/" }
"/regexp/(.*)/matching": { "to": "http://example.com/$1", regexp: true }
This format is more rich and works with any type of route, including routes served directly by the application.
Two keys are available under redirects
:
-
expires
: Optional, the duration that the redirect is cached. Examples of valid values include3600s
,1d
,2w
,3m
. -
paths
: The paths to apply the redirects to.
The following defines each rule under paths
:
- A key that describes the expression to match against the request path
- A value object describing both the destination to redirect to with detail on how to handle the redirection.
The value object is defined with the following keys:
to
Required, a partial ("/destination"
or "//destination"
) or full URL ("http://example.com/"
).
regexp
Optional, defaults to false
. Specifies whether the path key should be interpreted asa PCRE regular expression.
In the following example, a request to http://example.com/regexp/a/b/c/match
redirects to http://example.com/a/b/c
:
1
2
3
4
5
6
7
http://{default}/:
type: upstream
redirects:
paths:
"/regexp/(.*)/match":
to: "http://example.com/$1"
regexp: true
prefix
Specifies whether or not to redirect both the path and all its children or just the path itself. Defaults to true
, but is not supported if regexp
is true
.
For example,
1
2
3
4
5
6
7
http://{default}/:
type: upstream
redirects:
paths:
"/from":
to: "http://{default}/to"
prefix: true
In the preceding example, if prefix
is set to true
, /from
redirects to /to
and /from/another/path
will redirect to /to/another/path
If prefix
is set to false
, /from
triggers a redirect, but /from/another/path
does not.
append_suffix
Determines if the suffix is carried over with the redirect. Defaults to true
, but not supported if regexp
is true
or if prefix
is false
.
For example,
1
2
3
4
5
6
7
http://{default}/:
type: upstream
redirects:
paths:
"/from":
to: "http://{default}/to"
append_suffix: false
The preceding example results in /from/path/suffix
redirecting to just /to
.
If append_suffix
is set to its default value of true
, /from/path/suffix
redirects to /to/path/suffix
.
code
Specifies the HTTP status code. Valid status codes are 301
(Moved Permanently), 302
, 307
, and 308
. Defaults to 302
.
expires
Optional, the duration to cache redirect. Defaults to the expires
value defined directly under the redirects
key, but at this level you can fine-tune the expiration of individual partial redirects:
1
2
3
4
5
6
7
http://{default}/:
type: upstream
redirects:
expires: 1d
paths:
"/from": { "to": "http://example.com/" }
"/here": { "to": "http://example.com/there", "expires": "2w" }
In the preceding example, redirects from /from
expire in one day, but redirects from /here
expire in two weeks.