Set up multiple websites or stores

This topic discusses how to set up Magento Commerce to have multiple websites or stores. For example, you might have an English store, a French store, and a German store. For more information on websites, stores, and store views, see Understanding websites, stores, and store views.

To set up multiple stores, you must:

  1. Configure your local installation and test it locally.
  2. Configure Magento Commerce (Cloud) routes and variables.
  3. Push the changes to an Integration environment and test it.

Configure your local installation

To configure your local installation to use multiple stores, see Multiple websites or stores.

Configure your Integration environment

After successfully creating and testing multiple stores locally, you must:

  1. Configure routes, which specify how incoming URLs are handled by Magento Commerce.
  2. Set up websites, stores, and store views in your Magento Commerce server’s Admin.
  3. Modify magento-vars.php to specify the values of the MAGE_RUN_TYPE and MAGE_RUN_CODE variables.
  4. Deploy to your Integration branch and test.

Configure routes

Magento Enterprise Edition routes define how incoming URLs are processed. The way you configure routes depends on how you want your site to operate. We suggest configuring routes for integration as follows. You can edit the values later if your needs change.

To set up routes in a staging or production environment, you must create a Support ticket.</p>

To configure routes in an integration environment:

  1. Log in to your local environment as, or switch to, the Magento file system owner.
  2. Change to your Magento Commerce base directory.
  3. Open .magento/routes.yaml in a text editor.
  4. Replace its contents with the following:

    "http://{default}/":
    	type: upstream
    	upstream: "mymagento:php"
    
    "https://{default}/":
    	type: upstream
    	upstream: "mymagento:php"
    
    "http://*.{default}/":
    	type: upstream
    	upstream: "mymagento:php"
    
    "https://*.{default}/":
    	type: upstream
    	upstream: "mymagento:php"
    
  5. Save your changes to routes.yaml and exit the text editor.

Set up websites, stores, and store views

Set up in your Magento Commerce Admin websites, stores, and store views identical to the ones you set up on your local system.

Get your access information

To get the access information you need to log in to the Magento Admin:

  1. If you haven’t done so already, log in to your local environment as, or switch to, the Magento file system owner.
  2. Change to your Magento Commerce base directory.
  3. Log in to your account:

    magento-cloud login
    
  4. List the environments:

    magento-cloud environment:list
    
  5. Check out your environment:

    magento-cloud environment:checkout <environment ID>
    
  6. View the environment’s access URLs:

    magento-cloud environment:url
    
  7. View Admin login information:

    magento-cloud variable:list
    

    Admin access information displays similar to the following:

    +----------------+---------------+-----------+------+
    | ID             | Value         | Inherited | JSON |
    +----------------+---------------+-----------+------+
    | ADMIN_PASSWORD | admin_A456    | Yes       | No   |
    | ADMIN_URL      | magento_A8v10 | Yes       | No   |
    | ADMIN_USERNAME | meister_x2U8  | Yes       | No   |
    +----------------+---------------+-----------+------+
    

Configure websites, stores, and store views

Make sure you name your websites, stores, and store views in your Cloud Admin the same as you did when you set up your local installation.

See Set up multiple websites, stores, and store views in the Admin.

Modify magento-vars.php

Instead of configuring an nginx virtual host, pass the MAGE_RUN_CODE and MAGE_RUN_TYPE variables using magento-vars.php which is located in your Magento root directory.

  1. Open magento-vars.php in a text editor.
  2. Uncomment everything after the first two lines.
  3. Move the entire block starting with if (isHttpHost("example.com") after function isHttpHost($host).

    Following is what the file should look like so far:

    <?php
        // enable, adjust and copy this code for each store you run
        // Store #0, default one
        function isHttpHost($host)
        {
           if (!isset($_SERVER['HTTP_HOST'])) {
               return false;
        }
               return strpos(str_replace('---', '.', $_SERVER['HTTP_HOST']), $host) === 0;
        }
        if (isHttpHost("example.com")) {
            $_SERVER["MAGE_RUN_CODE"] = "default";
            $_SERVER["MAGE_RUN_TYPE"] = "store";
        }
  4. Change the following line:

    From:

    return strpos(str_replace('---', '.', $_SERVER['HTTP_HOST']), $host) === 0;
    

    To:

    return $_SERVER['HTTP_HOST'] ===  $host;
    
  5. Replace the following values in the if (isHttpHost("example.com")) block:

    • "example.com" with the base URL of your website, replacing the first period with three dashes.
    • "default" with the unique code for your website or store view.
    • "store" with either website (to load the website in the storefront) or store (to load a storeview in the storefront).

    An example follows:

    <?php
    // enable, adjust and copy this code for each store you run
    // Store #0, default one
    function isHttpHost($host)
    {
    	if (!isset($_SERVER['HTTP_HOST'])) {
    		return false;
    	}
    	 return $_SERVER['HTTP_HOST'] ===  $host;
    }
    if (isHttpHost("french---branch-sbg7pPa-f3dueAiM03tpy.us.magentosite.cloud")) {
    	$_SERVER["MAGE_RUN_CODE"] = "french";
    	$_SERVER["MAGE_RUN_TYPE"] = "website";
    }
    
  6. Save your changes to magento-vars.php and exit the text editor.

Deploy and test on the Integration server

The final step is to push your changes to your Magento Entperise Cloud Edition server and test your site there. To deploy and test:

  1. Enter the following commands in the order shown:

    git add -A && git commit -m "Implement multiple sites"
    git push origin <branch name>
    
  2. Wait for deployment to complete.
  3. When deployment is done, in a web browser, go to your site’s base URL.

    The URL must be in the format: http://<magento run_code>---<rest of URL>

    For example, http://french---master-benrmky-dyrozemqbw72k.us.magentosite.cloud/

  4. Make sure you test your site thoroughly.

When complete, merge the code to the master Git branch for further deployment.

Deploy to Staging and Production

Follow the deployment process for deploying to Staging and Production. For Starter and Pro environments, you use the Project Web Interface to push code across environments. For Pro accounts created before October 23, 2017 and not updated, you can use SSH and CLI commands.

We recommend fully testing in Staging prior to pushing to Production. If you need to make changes, you should complete those in Integration and beging the process to deploy across environments again.