Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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.

  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 { 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;


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