Setting Up Multiple Magento Stores with .htaccess Environment Variables
Introduction
Setting up multiple websites on a single Magento installation is a common requirement for e-commerce businesses. One of the most straightforward methods to achieve this involves using Apache environment variables defined in an .htaccess file. In this post, we’ll guide you through understanding and configuring these environment variables correctly.
Understanding Magento Environment Variables
Magento uses environment variables to differentiate between multiple stores running on the same installation. The two primary variables are MAGE_RUN_CODE and MAGE_RUN_TYPE.
- MAGE_RUN_CODE: Specifies the store code for which the current request is made.
- MAGE_RUN_TYPE: Defines whether the request is for a website (
website) or a store view (store).
Configuring .htaccess Environment Variables
The .htaccess file provides a simple yet powerful way to set these environment variables based on the host header of the incoming request.
Syntax Overview
The SetEnvIf directive in Apache allows you to check if a specified condition matches and, if so, sets an environment variable. Here’s the general syntax:
SetEnvIf <condition> <name>="<value>"
In our context, the condition is typically a regular expression matching the domain or host header of the incoming request.
Example: Single Regex Match
Let’s consider you have two domains - www.domain.com (main store) and www.domain1.com (additional store). The Magento installation is located on domain.com.
SetEnvIf Host ^(www\.)?domain\.com$ MAGE_RUN_CODE="base";
SetEnvIf Host ^(www\.)?domain1\.com$ MAGE_RUN_TYPE="website";
In this example:
- The first line sets the
MAGE_RUN_CODEvariable to “base” for all requests matching eitherwww.domain.comordomain.com. This is your main store. - The second line sets the
MAGE_RUN_TYPEvariable to “website” for all requests matchingwww.domain1.com.
Regex Matches for Multiple Domains
If you have multiple domains and subdomains, you can use regular expressions to match them all. For instance:
SetEnvIf Host ^(www\.)?domain\.com$ MAGE_RUN_CODE="base";
SetEnvIf Host ^(www\.)?(domain1|subdomain2)\.com$ MAGE_RUN_TYPE="website";
This configuration matches:
- All requests for
domain.comorwww.domain.com, setting the store code to “base”. - All requests for
domain1.com,www.domain1.com,subdomain2.com, orwww.subdomain2.com, setting the run type to “website”.
Conclusion
Setting up multiple Magento stores using .htaccess environment variables is a clean and efficient solution. By properly configuring your regular expressions, you can ensure that each store is directed to the correct codebase without needing separate installations.
Remember, the key is to match the host header of the incoming request with the appropriate regex pattern for each domain or subdomain. This allows Magento to dynamically switch between stores based on the user’s URL.
For more advanced configurations and best practices in managing multiple Magento stores, be sure to check out our comprehensive guide at LaravelCompany.
Happy coding!