3

We have a Magento 2.4 project with multiple websites with multiple stores & we need to share some "store codes" in url terms for store views. All store views should load under same domain

Document root of this project is pointing to magento/pub folder, as recommended

URL structure should be a bit different than usual

I'll try to explain using some concrete url strings, so answers can use them, for clarity

Website1 (es)
------------- Store1 -------- Storeview (es_hair)
              Store2 -------- Storeview (es_pets)
Website2 (fr)
------------- Store1 -------- Storeview (fr_hair)
              Store2 -------- Storeview (fr_pets)

They should run under this urls

www.domain.com/es/hair/ 
www.domain.com/es/pets/
www.domain.com/fr/hair/
www.domain.com/fr/pets/

How can we achieve this?

Using nginx configuration would NOT be the desired way. We'd like to follow some approach like this using folders but we've tried with no success, so I open this question to try understanding this better. Please use the provided concrete urls in your answers so we can try. Apart from the filesystem changes I need to know too the required configuration changes in admin

UPDATE

We have this working but still some doubts. What we've done:

We have created this folders at magento/pub folder

/es/hair
/es/pets
/fr/hair
/fr/pets

In this folders we have copied both .htaccess & index.php files from magento/pub folder

.htaccess file has the same code (first doubt: something to do with RewriteBase here?)

In index.php file we have changed the path to bootstrap.php & we have added this parameters, so we force Magento to load a concrete storeview

$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] <- code of the storeview here
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store';

We have added 2 symlinks media & static to magento/pub/media & magento/pub/static folders

With this the website and all store views seem to be loading fine by setting global parameter "use store code in urls" : "no" & setting base_url to www.domain.com/es/hair/ and so on for every storeview

Another doubt, which files should we add in first level folders (/es, /fr)? I understand we'd need some .htaccess file there

And the last & big doubt: would this be enough? or are we missing something?

0

2 Answers 2

2
+200

Multi-Store Setup (Apache)

Create a virtual host for your main example.com

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/{virtualname}com.conf

Here {virtualname} is the file name you can add any name as you need. Our hostname is an example then we used that name.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/examplecom.conf

After creating please open that file. And add below code content(must be open with root user):

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example/pub

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/html/example/pub>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          allow from all
    </Directory>

</VirtualHost>

Now we are enabling this site file using the below command:

sudo a2ensite examplecom.conf

Add your new hostname into the system hosts file using the below command:

sudo nano /etc/hosts

After opening the file please add this:

127.0.0.1 example.com
127.0.0.1 www.example.com

When you enable any site file or add a new host then you must restart the service using the below command:

sudo systemctl reload apache2

Ubuntu: service apache2 restart

CentOS: service httpd restart

After the above process http://example.com/ URL is working. Now we set up a multi-store URL. as per your requirement multi-store folders are created into /var/www/html/example/pub directory. Here we are creating two folders wholesale and retail.

cd /var/www/html/example/pub

mkdir wholesale

mkdir retail

After making the directory, we need to copy the index.php and .htaccess file from /var/www/html/example/pub directory and place it into your newly created folders (wholesale and retail).

index.php

<?php

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;

try {
    require __DIR__ . '/../../app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

$params = $_SERVER;
$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = array_replace_recursive(
    $params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] ?? [],
    [
        DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
        DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
        DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
        DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
    ]
);
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);

Do not copy the root .htaccess file. Copy pub directory .htaccess file. And add the below line:

# SET MULTI-STORE CODE NAME

SetEnvIf Host .*example.* MAGE_RUN_CODE=YourWebsiteCode
SetEnvIf Host .*example.* MAGE_RUN_TYPE=website

enter image description here

Get your website code. Go Stores -> Configuration -> Settings -> All Stores

Now you must restart the service using the below command:

sudo systemctl reload apache2

Ubuntu: service apache2 restart

CentOS: service httpd restart

Note: You must be added the URL in your Magento core_config_data table.

http://example.com/
http://example.com/wholesale/
http://example.com/retail/
2
  • Thanks for the answer, I will accept this. But, can you read the update I added & answer the concrete doubts? Oct 6, 2022 at 5:00
  • Yes, that's enough. And I posted above which files should be placed, and on which path.
    – Msquare
    Oct 6, 2022 at 6:05
-1

IMO it's ugly but you can add directory in pub directory like:

pub/store_code/index.php

Duplicate the main index.php in all new directories and add that:

use Magento\Framework\App\Bootstrap;

try {
    require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'your_store_ce';

$bootstrap = Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);
1
  • Sorry, but that's practically the same I posted in the UPDATE part. By the way, you don't answer the doubts I ask in the question Oct 4, 2022 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.