Setting Up Multiwebsite, Multiple Domain in Magento 2 After Migration
Migrating from Magento 1 to Magento 2 is a significant step that requires careful planning and execution. One of the challenges you might face after migration is setting up multiple websites or domains correctly.
Understanding Your Current Setup
Your original Magento 1 installation consisted of:
- 1 Website: This is the highest level in the Magento hierarchy.
- 2 Stores: These are assigned to a specific website and share many settings.
- 4 Storeviews: Each store has two storeviews for different languages (NL/EN, NL/FR).
In Magento 2, this structure translates to:
- 1 Website Code (‘base’): This is the default code for your main website.
- 2 Stores: You will have one store per domain.
- 4 Storeviews: Each storeview corresponds to a language.
Key Considerations
Before we dive into troubleshooting, let’s understand some key points:
- Magento 2 uses a single index.php file in the root directory for all domains.
- The domain name is used to determine which website/store to load.
- You don’t need separate index.php files for each domain unless you have multiple websites (with different website codes).
Troubleshooting Your Current Approach
You’ve tried several methods, but let’s analyze why these didn’t work:
-
Editing index.php: The code you provided is almost correct, but it’s missing the actual parameter assignment for the store code and type.
-
Creating separate index.php files: Magento 2 handles this differently. It uses a single entry point (index.php) with conditional logic to determine which store to load based on the domain name.
-
Using store-codes: You can’t directly use store-codes in your condition because the system first determines the website and then maps it to a storeview.
-
Editing .htaccess files: This isn’t necessary for this purpose. Magento 2 handles domain routing internally.
The Correct Approach
Here’s how you should set up multiple domains with one Magento 2 installation:
-
Create your stores: In the Magento admin, navigate to Stores > Settings > All Stores. Click “Add New Store” and create a new store for each domain.
-
Set base URLs: Ensure both stores have their correct base URLs pointing to the same root directory (i.e., /).
-
Configure language settings: Assign the appropriate storeviews (language combinations) to each store.
-
Edit index.php: Open your root directory’s index.php file and add the following conditional logic:
$params = $_SERVER;
switch($_SERVER['HTTP_HOST']) {
case 'spijl-in-stijl.be':
case 'www.spijl-in-stijl.be':
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'nl';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store';
break;
default:
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = '';
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
}
- Flush Magento cache: After making changes, clear the cache from the admin (System > Cache Management).
Conclusion
Setting up multiple domains with a single Magento 2 installation requires a deep understanding of how the system handles domain routing.
Remember, all your stores share the same codebase and files. The key is to use the HTTP_HOST variable in the index.php file to conditionally set the run_code and run_type parameters correctly based on the domain name.
If you’re still facing issues or need more detailed technical guidance, feel free to reach out to our team at Laravel Company. We specialize in Magento 2 development and migration services.