Skip to content

Instantly share code, notes, and snippets.

@garyrojas
Last active February 16, 2022 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save garyrojas/2927213fe3de86169594bca2e728967a to your computer and use it in GitHub Desktop.
Save garyrojas/2927213fe3de86169594bca2e728967a to your computer and use it in GitHub Desktop.
Magento 2 and Nginx multiple websites looks as sub directories per website

Magento 2/Nginx Multi Website in URL segment or subdirectories

Magento has the solution by default but the project should is in the root but if your project is in a sub-folder then you will have a problem recognizing the code new websites.

For Apache the solution is an index.php and .htaccess but if you using Nginx then follow the next steps:

  1. Create root categories
  2. Create websites
  3. Create stores
  4. Create store views
  5. Add the store code to the base URL
  • In the Admin panel, click Stores > Settings > Configuration > General > Web.
  • In the right pane, expand Url Options.
  • Clear the Use system value checkbox next to Add Store Code to Urls.
  • From the Add Store Code to Urls list, click Yes.
  • Click Save Config.
  • If prompted, flush the Magento cache. (System > Cache Management).

For the details steps mentioned you can check this articule. https://devdocs.magento.com/guides/v2.3/config-guide/multi-site/ms_websites.html

  1. Finally we need define the code website per URL segment.
  • Create a map in your domain.conf /etc/nginx/conf.d/website.conf.

- map website base

map $http_host $MAGE_RUN_CODE {
    dev.mywebsite.com base;
}

- map X aditional website. This tells Magento which website code to load.

map $http_host$uri $MAGE_RUN_CODE {
        ~*^(dev\.)?mywebsite\.com/website1/.*  website1;
        ~*^(dev\.)?mywebsite\.com/website2/.*  website2;
}

or

map $request_uri $MAGE_RUN_CODE {
    default default;
    ~^/website1/.*  website1;
    ~^/website2/.*  website2;
}
  1. sudo service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment