How to configure different redirects in this file, which types of redirects should not be configured here
A route describes how an incoming HTTP request is going to be processed by Magento Cloud. Route configuration (from platform.sh)
File .magento/routes.yaml
- defines routes for the Integration environments.
"http://{default}/":
type: upstream
upstream: "mymagento:php"
# whole-route redirects
"http://www.{default}/":
type: redirect
to: http://{default}/
# partial redirects
http://test.{default}/:
type: upstream
redirects:
expires: 1d
paths:
"/from": { "to": "/destination", code: 308, expires: 2w}
"/regexp/(.*)/matching": { "to": "http://example.com/$1", regexp: true }
Each rule under paths
is defined by:
key - expression to match against the request path;
value - object describing both the destination to redirect with detail on how to handle the redirection;
Value object is defined with the following keys:
- to: (required) partial ("/destination" or "//destination") or full URL ("http://example.com/").
- regexp: (default: false) specifies whether the path key should be interpreted as a PCRE regular expression
Special arguments in the
to
statement (only if regexp: true)$is_args
:?
or empty string$args
: full query string if any$arg_foo
: value of the query parameterfoo
$uri
: full URI of the request
- prefix: (default: true, not supported if regexp is true) specifies whether we should redirect both the path and all its children or just the path itself Example: if true: /from redirects to /to and /from/another/path will redirect to /to/another/path if false: /from triggers a redirect, but /from/another/path does not.
- append_suffix: (default: true, not supported if regexp is true or if prefix is false) determines if the suffix is carried over with the redirect Example: if true: /from redirects to /to/path/suffix in case /from/path/suffix if false: /from redirects to /to in case /from/path/suffix
- code: status codes are 301, 302 (default), 307, and 308
- expires: examples of valid values include 3600s, 1d, 2w, 3m
List the configured routes: magento-cloud environment:routes
Which types of redirects should not be configured here:
- Application-driven redirects
- Fastly
- Long list of storefront domains (not sure)
- As a general rule we recommend keeping the defined routes under 100
- Let's Encrypt also limits an environment to 100 configured domains
- Linux kernel limit on environment variables is 32 pages (each page is 4k on x86, so maximum environment variable length of 128KB). So it's a limits for
routes.yaml
How to add these configurations to Staging or Production environments. Magento On-Premises installation migration
.magento/routes.yaml
limitations:
For Pro projects, the changes you make using this YAML file affects your Integration environment only. To make these changes in a Staging or Production environment, you must create a Support ticket. (https://devdocs.magento.com/guides/v2.3/cloud/project/project-conf-files_routes.html)
Other
You must submit a support ticket to update and modify the following in the Staging and Production environments:
- Cron jobs
- Redirects from routes.yaml file
- Managing PHP extensions
- Managing mounts (https://devdocs.magento.com/guides/v2.3/cloud/trouble/pro-env-management.html)
Also it's possible to create redirect via Fastly VCL snippets:
- Set the custom response code related to redirects
- Name: student_redirect
- Type: recv
- Priority: 100
- VCL snippet content:
if (req.http.host ~ "student.test.com"") { error 750 "student.test.com""; }
- VCL that contains all needed redirects:
- Name: custom_domains_redirect
- Type: error
- Priority: 100
- VCL snippet content:
if (obj.status == 750) { if (obj.response ~ "student.test.com") { set obj.http.Location = "https://test.com/student"; } set obj.status = 302; set obj.response = "Moved Temporarily"; return (deliver); }
Example how set up redirects to WordPress using Fastly
VCL snippet for the WordPress redirect
{
"name": "wordpress_redirect",
"dynamic": "0",
"type": "recv",
"priority": "5",
"content": "if ( req.url.path ~ \"^\\/?([^\\/?]+)\") { if ( table.lookup(wordpress_urls, re.group.1, \"NOTFOUND\") != \"NOTFOUND\" ) { set req.http.X-WP = \"1\"; } }"
}
Another way to set up redirects - use edge dictionary. Working with Edge Dictionary items using the API
How to migrate an existing Magento installation into Magento Cloud: Code base, database, media migration
Code base
- add required files (examples from magento-cloud repo)
.magento.app.yaml - manages applications, service relationships, mounts for writable directories, and cron jobs .magento/services.yaml - for service configurations including MySQL, PHP, Redis, Solr (2.0.X only), ElasticSearch (2.1.X and later) .magento/routes.yaml - for handling routes including redirections, caching, and server-side includes magento-vars.php - for multiple websites and stores
- add Magento authentication keys and custom repositories keys
- using
auth.json
file{ "http-basic": { "repo.magento.com": { "username": "<public-key>", "password": "<private-key>" } } }
- using an environment variable (
env:COMPOSER_AUTH
)- project level variable
- name: env:COMPOSER_AUTH.
- content the same as in
auth.json
file - visible during build: true
- visible at run: false
- using
- edit composer.json
- replace
magento/product-enterprise-edition
to"magento/magento-cloud-metapackage": "<version>",
- update the "files" directive in the autoload section to refer to app/etc/NonComposerComponentRegistration.php (why?)
- run composer update to update composer.lock
- replace
- encryption key
- get old encryption key from /app/etc/env.php (crypt/key)
- add variable
CRYPT_KEY
with correct key - or update crypt/key manually on each env (don't like it)
- see ece-tools Process/Deploy/SetCryptKey.php
Database
- create a database dump:
mysqldump -h <db-host> -P <db-port> -p -u <db-user> <db-name> --single-transaction --no-autocommit --quick | gzip > ~/db.sql.tgz
- transfer the database dump to the Magent Cloud
rsync db.sql.tgz <cloud SSH URL>:var/db.sql.tgz
- ssh to needed env
magento-cloud ssh
- connect to the database
mysql -h <db-host> -P <db-port> -p -u <db-user> <db-name>
- drop the database
drop database <db-name>;
- re-create the database
create database <db-name>;
- restore dump
zcat var/db.sql.tgz | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | mysql -h <db-host> -P <db-port> -p -u <db-user> <db-name>
- update configs (
SELECT * from core_config_data;
)- base URLs
UPDATE core_config_data SET value='<Cloud unsecure base URL>' WHERE path='web/unsecure/base_url';
UPDATE core_config_data SET value='<Cloud secure base URL>' WHERE path='web/secure/base_url';
- base URLs
Media migration
- back up media files
php <Magento Commerce install dir>/bin/magento setup:backup --media
- transfer the media dump
rsync <Magento Commerce install dir>/var/backups/<backup file name> <cloud ssh url>:var/media.tgz
- ssh to needed env
magento-cloud ssh
- clear existing media files
rm -rf pub/media/*
- extract the media files to the pub/media directory
tar -xzf var/media.tgz pub/media
What to configure in this file, on which environments these configurations are applied, how to add these configurations to environments where this file is not read
Documentation: