Multisite Configuration
Understanding routes.yaml, .magento.app.yaml, magento-vars.php, MAGE_RUN_CODE, and multisite setup.
Multisite Configuration
Multisite Configuration Overview
Three Key Configuration Files
.magento/routes.yaml
Routes subdomains to Magento application
.magento.app.yaml
Configures web/locations for subfolder sites
magento-vars.php
Sets MAGE_RUN_CODE and MAGE_RUN_TYPE
Routes Configuration (.magento/routes.yaml)
Setting Up Subdomain Routing
Purpose
If you intend to set up Magento stores with subdomains, you need to route these back into the main Cloud backend.
This happens in .magento/routes.yaml.
Wildcard Subdomain Configuration
# Using wildcard (*) for any subdomain
"http://*.{default}/":
type: upstream
upstream: "mymagento:php"
"https://*.{default}/":
type: upstream
upstream: "mymagento:php"
What This Does:
- Routes any subdomain to Magento application
b2b.example.com→ Magentob2c.example.com→ Magentowholesale.example.com→ Magento- All subdomains handled by same Magento instance
Hardcoded Subdomain Configuration
# Hardcoding specific subdomains
"http://b2b.{default}/":
type: upstream
upstream: "mymagento:php"
"http://b2c.{default}/":
type: upstream
upstream: "mymagento:php"
"https://b2b.{default}/":
type: upstream
upstream: "mymagento:php"
"https://b2c.{default}/":
type: upstream
upstream: "mymagento:php"
Upstream Reference
Note that mymagento is specified in .magento.app.yaml in the name node.
# .magento.app.yaml
name: mymagento
type: php:8.2
.magento.app.yaml Configuration
Subfolder Sites Configuration
Purpose
To configure Magento to serve different sites as a subfolder, you need to update the web/locations configuration in .magento.app.yaml.
Example: /b2b Subfolder Configuration
web:
locations:
"/":
root: "pub"
passthru: "/index.php"
index:
- index.php
scripts: true
allow: false
rules:
\.(css|js|map|hbs|gif|jpe?g|png|tiff|wbmp|ico|jng|bmp|svgz|midi?|mp?ga|mp2|mp3|m4a|ra|weba|3gpp?|mp4|mpe?g|mpe|ogv|mov|webm|flv|mng|asx|asf|wmv|avi|ogx|swf|jar|ttf|eot|woff|otf|html?)$:
allow: true
^/sitemap(.*)\.xml$:
passthru: "/media/sitemap$1.xml"
"/media":
root: "pub/media"
allow: true
scripts: false
passthru: "/get.php"
"/static":
root: "pub/static"
allow: true
scripts: false
passthru: "/front-static.php"
rules:
^/static/version\d+/(?.*)$:
passthru: "/static/$resource"
# B2B subfolder configuration
"/b2b":
root: "pub"
passthru: "/index.php"
index:
- index.php
scripts: true
allow: false
magento-vars.php - The Powerful Router
Understanding magento-vars.php
Power of magento-vars.php
This is a powerful file that is run before pub/index.php.
Capabilities:
- Configure most (all?) server, post, and get variables going into Magento
- Saves you from modifying pub/index.php
- No need to maintain modifications through composer updates
- Has access to everything in
$_SERVERsuperglobal - Can access environment variables
File Location
Place this file in the root directory (same level as pub/, app/, etc.)
$_SERVER Variables Available
Environment Variables in magento-vars.php
Example URL
http://staging-55555-vvvvvvv.us-4.magentosite.cloud/b2b/
Available $_SERVER Variables:
| Variable | Example Value | Description |
|---|---|---|
MAGENTO_CLOUD_APPLICATION |
Base64-encoded JSON | Base64-encoded version of .magento.app.yaml |
MAGENTO_CLOUD_ROUTES |
Base64-encoded JSON | Base64-encoded version of all routes |
HTTP_HOST |
staging-55555-vvvvvvv.us-4.magentosite.cloud | Hostname of the request |
HTTP_X_ORIGINAL_ROUTE |
http://{default}/ | Original route template |
DOCUMENT_URI |
/b2b/index.php | Current document URI |
DOCUMENT_ROOT |
app/pub/ | Document root directory |
magento-vars.php Routing Example
Setting MAGE_RUN_CODE and MAGE_RUN_TYPE
Basic B2B Routing Example
<?php
// magento-vars.php
if (isset($_SERVER['DOCUMENT_URI']) &&
stripos($_SERVER['DOCUMENT_URI'], '/b2b/') !== false) {
$_SERVER["MAGE_RUN_CODE"] = "b2b";
$_SERVER["MAGE_RUN_TYPE"] = "website";
}
What This Does:
- Checks if URL contains
/b2b/ - Sets
MAGE_RUN_CODEto "b2b" (website code) - Sets
MAGE_RUN_TYPEto "website" - Magento loads the B2B website configuration
Advanced Subdomain Routing
<?php
// magento-vars.php
// Route based on subdomain
if (isset($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
// B2B subdomain
if (strpos($host, 'b2b.') === 0) {
$_SERVER["MAGE_RUN_CODE"] = "b2b";
$_SERVER["MAGE_RUN_TYPE"] = "website";
}
// B2C subdomain
if (strpos($host, 'b2c.') === 0) {
$_SERVER["MAGE_RUN_CODE"] = "b2c";
$_SERVER["MAGE_RUN_TYPE"] = "website";
}
// Wholesale store
if (strpos($host, 'wholesale.') === 0) {
$_SERVER["MAGE_RUN_CODE"] = "wholesale";
$_SERVER["MAGE_RUN_TYPE"] = "store";
}
}
Debugging: Print $_SERVER Variables
<?php
// magento-vars.php
// Use this to understand available variables
print_r($_SERVER);
exit;
Debugging Only!
Remove print_r($_SERVER) after debugging! Never leave this in production.
Complete Multisite Setup Workflow
Step-by-Step Implementation
Step 1: Create Websites in Magento Admin
- Navigate to Stores → All Stores
- Click Create Website
- Enter website details:
- Name: B2B
- Code: b2b
- Create store and store view under website
- Configure website settings
Step 2: Dump Configuration
./vendor/bin/ece-tools config:dump
This exports website/store configuration to app/etc/config.php
Step 3: Configure Routes (.magento/routes.yaml)
"http://b2b.{default}/":
type: upstream
upstream: "mymagento:php"
"https://b2b.{default}/":
type: upstream
upstream: "mymagento:php"
Step 4: Configure magento-vars.php
<?php
if (isset($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
if (strpos($host, 'b2b.') === 0) {
$_SERVER["MAGE_RUN_CODE"] = "b2b";
$_SERVER["MAGE_RUN_TYPE"] = "website";
}
}
Step 5: Commit and Deploy
git add .magento/routes.yaml magento-vars.php app/etc/config.php
git commit -m "Add B2B multisite configuration"
git push origin integration
Step 6: Sync Data Across Environments
Important: Don't forget updating app/etc/config.php in production and then syncing data back to staging and integration.
- Create stores in production admin
- Run config:dump in production
- Pull config.php from production to staging/integration
- Or: Sync database from production to lower environments
MAGE_RUN_CODE and MAGE_RUN_TYPE
Understanding Run Parameters
| Parameter | Purpose | Values |
|---|---|---|
MAGE_RUN_CODE |
Code of website or store to load | Website code (e.g., "b2b", "b2c") or Store code |
MAGE_RUN_TYPE |
Type of entity to load | "website" or "store" |
Examples
// Load B2B website
$_SERVER["MAGE_RUN_CODE"] = "b2b";
$_SERVER["MAGE_RUN_TYPE"] = "website";
// Load specific store
$_SERVER["MAGE_RUN_CODE"] = "german_store";
$_SERVER["MAGE_RUN_TYPE"] = "store";
Practical Experience Tips
Hands-On Learning
Recommended Exercises
- Configure multiple stores: Create at least 2 websites in admin
- Use print_r($_SERVER): In magento-vars.php to understand available variables
- Configure /b2b URL path: Set up subfolder routing
- Test subdomain routing: Configure b2b.domain.com
- Sync config.php: Practice syncing between environments
Common Configurations
Popular Multisite Patterns
Pattern 1: Subdomain per Website
URLs:
- b2b.example.com
- b2c.example.com
- wholesale.example.com
Configuration:
- Wildcard route in routes.yaml
- HTTP_HOST detection in magento-vars.php
- MAGE_RUN_TYPE = "website"
Pattern 2: Subfolder per Website
URLs:
- example.com/b2b/
- example.com/b2c/
- example.com/wholesale/
Configuration:
- web/locations in .magento.app.yaml
- DOCUMENT_URI detection in magento-vars.php
- MAGE_RUN_TYPE = "website"
Pattern 3: Different Domains
URLs:
- b2bstore.com
- b2cstore.com
Configuration:
- Hardcoded routes in routes.yaml
- HTTP_HOST detection in magento-vars.php
- DNS pointing to Cloud environment
Troubleshooting
Common Issues
Issue 1: Subdomain Not Routing
Solution:
- Verify routes.yaml has subdomain configured
- Check upstream name matches .magento.app.yaml
- Redeploy after routes.yaml changes
- Check DNS points to Cloud environment
Issue 2: Wrong Website Loading
Solution:
- Debug: Add print_r($_SERVER) in magento-vars.php
- Verify MAGE_RUN_CODE matches website code in admin
- Check if condition in magento-vars.php is correct
- Clear cache: bin/magento cache:flush
Exam Tips
Key Points to Remember
- Routes file: .magento/routes.yaml for subdomain routing
- Wildcard: Use * for any subdomain
- Upstream: Must match name node in .magento.app.yaml
- Subfolder config: Update web/locations in .magento.app.yaml
- magento-vars.php: Runs BEFORE pub/index.php
- $_SERVER access: magento-vars.php has full $_SERVER access
- MAGE_RUN_CODE: Website or store code
- MAGE_RUN_TYPE: "website" or "store"
- Key variables: HTTP_HOST, DOCUMENT_URI, MAGENTO_CLOUD_ROUTES
- Config sync: Update config.php in production, sync to lower environments
- Debugging: Use print_r($_SERVER) to see available variables