Skip to content

Instantly share code, notes, and snippets.

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

@meetvora2006

This comment has been minimized.

Copy link

meetvora2006 commented Sep 21, 2017

Awesome!! thanks, it is so much helpful

@meetvora2006

This comment has been minimized.

Copy link

meetvora2006 commented Sep 22, 2017

I am here to ask for help, I can share customers between two websites using customer sharing as global. do you have any idea how can I share shopping cart between two websites? I followed the steps you mentioned above and created multi site with websites. any help would be much appreciated. I am very new to magento2. I read here we have to share SID session between two stores, but not sure how to do it.
https://magento.stackexchange.com/questions/3617/sharing-sessions-between-stores-with-different-domains/3624#3624

I tried this $_COOKIE['website'] = $_SERVER['MAGE_RUN_CODE']; putting in my index.php of new website but did not work for me. any help or new article will be much appreciated. there is no article for magneto2 for sharing shopping cart between multi site out there.

Thank you so much for your time.

@king-themes

This comment has been minimized.

Copy link

king-themes commented Oct 10, 2017

Very helpful, but. I define luna as theme for the new website.
It actually loads the new website but without css and js.
Is it something I made wrong?

@hammockvienna

This comment has been minimized.

Copy link

hammockvienna commented Dec 4, 2017

I did

  • a symbolic link and no subdirectory
  • a case statement within the index.php
    this is the advantaget that i have just 1 index.php to maintain.
@antoniogarcia78

This comment has been minimized.

Copy link

antoniogarcia78 commented Feb 13, 2018

Any install using 2 level directory? as /jp/jp/

@benyang2015

This comment has been minimized.

Copy link

benyang2015 commented Mar 13, 2018

can't work,
magento2.2.2
nginx

@benyang2015

This comment has been minimized.

Copy link

benyang2015 commented Mar 13, 2018

can load css and js
how to do

@zhenyusun

This comment has been minimized.

Copy link

zhenyusun commented Apr 9, 2018

I need to have mysite.com/en url for a storeview, not a website. I did exactly the same as the instruction. only changed PARAM_RUN_TYPE from website to store. but I got 404 page when visiting mysite.com/en.

I did flush cache and reindexing.

Magento 2.2.2

@mariamghalleb

This comment has been minimized.

Copy link

mariamghalleb commented Apr 18, 2018

Thank you for sharing this solution it worked for store views. But not for websites, any idea?

@mikelevy300

This comment has been minimized.

Copy link

mikelevy300 commented Jun 4, 2018

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.

@jackeryhammond

This comment has been minimized.

Copy link

jackeryhammond commented Aug 28, 2018

I've followed thagxt and mikelevy300's instructions and both break the theme for both stores example.com and example.com/store2/. I am at a loss as to how to fix this.

@zajd

This comment has been minimized.

Copy link

zajd commented Sep 27, 2018

I've followed thagxt and mikelevy300's instructions and both break the theme for both stores example.com and example.com/store2/. I am at a loss as to how to fix this.

Check your dev tools console, are you receiving 404 for theme assets? You can either set the static and media urls as mikelevy300 suggested

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.

You can also (if you prefer) simply add a symlink in your new "web roots" (ie subdirectories) to the pub folder in the main install location. Something like

cd <full-path-to-install-root>
ln -s pub <subdirectory>/

@just-tom

This comment has been minimized.

Copy link

just-tom commented Sep 24, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.