Magento2.4.2 multi website setup not working
-
15-04-2021 - |
Question
I have upgraded the Magento version from 2.3.3 to M2.4.2 now the main example.com
working fine and example.com/wholesale
| example.com/retail
not working. I have copied .htaccess
from pub to these subfolders but it's not working. following errors are throwing:
Refused to apply style from '<URL>' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Here is index.php
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'wholesale';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
Solution
Magento 2.4.2 now enforces the best practice of serving all content from magento_root/pub.
Most sites won't notice this change because of the rewrites in the 2.4.2 version of magento_root/.htaccess
https://github.com/magento/magento2/commit/640cad53009b291334234ccd61ab79f256b43da2
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule .* /pub/$0 [L]
DirectoryIndex index.php
These rewrites will have no effect on stores served from sub folders which will now be broken.
The simplest way to load one or more stores via url sub folders is to create the sub folder in magento_root/pub
and symlink to the static pub folders/files.
For example
mkdir magento_root/pub/wholesale
cd magento_root/pub/wholesale
ln -s ../media media
ln -s ../static static
ln -s ../../pub pub
ln -s ../.htaccess .htaccess
ln -s ../health_check.php health_check.php
magento_root/pub/wholesale/index.php
<?php
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'wholesale';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);