Skip to content

Instantly share code, notes, and snippets.

@thagxt
Last active May 2, 2024 14:03
Show Gist options
  • Save thagxt/0f605f0a8a95c79302db0d2f04383788 to your computer and use it in GitHub Desktop.
Save thagxt/0f605f0a8a95c79302db0d2f04383788 to your computer and use it in GitHub Desktop.
Set up Magento 2 multiple websites in sub directories

Set up Magento 2 multiple websites in sub directories

  1. Go to Admin > Stores > All Stores
  2. Click > Create Web Site
  3. In the Name field, enter store name.
    • e.g. Japan
  4. In the Code field, enter a unique string without spaces and > Save Web Site
    • e.g. super_jp
  5. Create Store
  6. Create Store View
  7. Go to Stores > Configuration
  8. Select the website we just created.
  9. Go to Web & change both Base URLs & Base URLs (Secure)
    • e.g. Base URL: http://example.com/
    • Base Link URL: {{unsecure_base_url}}jp/
    • ... same goes for Base URLs (Secure)
  10. Save. Clear Cache. Reindex.

Doing stuff in server root

  1. Create a subfolder with the desired name.

    • e.g. /jp/
  2. Copy index.php & .htaccess from root (!important copy both files)

  3. Open index.php clear everything inside it and paste:

    require realpath(__DIR__) . '/../app/bootstrap.php';
    $params = $_SERVER;
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'super_jp'; // change this with the code you chose in step. 4
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; // store or website
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
    
    $app = $bootstrap->createApplication('Magento\Framework\App\Http');
    $bootstrap->run($app);
    
  4. Save. Clear Cache. Reindex.

  5. Enjoy your new website inside a subdir.

@just-tom
Copy link

Hi guys,

I have followed thagxt and mikelevy300's but to no avail.

What we are trying to achieve is a single domain installation with multiple websites. So domains will look like this: example.com && example.com/websitecode && example.com/websitecode/languagecode

The difference being we are using nginx. We are setting the run type etc and its being set fine. BUT whatever we try, all pages for /websitecode is consistently showing 404 pages no matter what we do.

Anyone else had this?

I created a cms page specifically for the homepage of the custom website and set it in config but that didnt help either.

Thanks

@LucasMali
Copy link

LucasMali commented Feb 26, 2020

So it seems to work, but like others, it kills the theme. Is there any way to get this working with theme versioning? e.g. Sign Static Files

@garyrojas
Copy link

@fcpeomaior
Copy link

Didn't work for me on magento CE 2.3.4. Questions I have:

  • in step 11 when it says to create folder /jp/ where should I place it? if magento is installed in "/var/www/html/magento" should I create folder in "/var/www/html/magento/jp/"?
  • is there any additional config I need to do? when I select the url (https://mydomain.com/jp/) I just get the raw text I copied in step 13
    Any suggestions?
    Thanks

@thagxt
Copy link
Author

thagxt commented Jul 11, 2020

Sorry guys I no longer work with Magento. Unfortunately I can't help you

@sshymko
Copy link

sshymko commented Mar 18, 2021

It's possible to implement the dynamic store resolution from URL path via Nginx map by using a RegEx capturing group to hold the parsed store code. Complex substitution expressions, such as "b2c_$store", are supported since Nginx 1.11.2, whereas simple ones like "$store" will work even on earlier versions.

map $host$request_uri $MAGE_RUN_CODE {
    default b2c_en;
    ~^example\.com/(?<store>.*?)/ "b2c_$store";
    ~^wholesale\.example\.com/(?<store>.*?)/ "b2b_$store";
}

The RegEx can strictly list all known store codes, fo instance:

(?<store>en|au|nz)

Originally posted as a comment to the article http://disq.us/p/2fsxi0u.

@Osara12
Copy link

Osara12 commented May 18, 2021

For me this is how I got websites working.

I've got www.example.com pointing to my doc root where the code base lives. In the doc root, I've got a subdir named sub1. In Magento, I've created a new website and set the base URLs for the new website to www.example.com/sub1/. I also had to set the static and media files base URLs to 'www.example.com/pub/static/' and 'www.example.com/pub/media/', respectively, otherwise the css and js wouldn't load.

Then like in the original solution, copy index.php and .htaccess into /sub1, open up the new index.php and copy the code from the original post, being sure to set PARAM_RUN_TYPE to 'website' and PARAM_RUN_CODE to the new website's code. Flush the cache and enjoy.

Thank you for sharing. I can create multiple websites by using this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment