Solving Magento Store Redirection Issues
The Scenario Explained
You’re facing a common challenge when dealing with multi-domain setups using the Magento e-commerce platform. You have two domainsâDomainA.nl (with the Magento installation) and DomainB.nl (with just an .htaccess file). Your goal is to redirect traffic from DomainB.nl to DomainA.nl while loading the desired store view.
Understanding the Current Setup
Let’s break down your current configuration:
-
DomainB.nl: This domain has a simple .htaccess file that redirects all requests to
http://www.DomainA.nl. -
DomainA.nl: This is the primary Magento installation with two stores (Store 1 and Store 2). Store 1 contains two store viewsâ
mbv_nl(the default) andmbv_en. Store 2 has only one store view,test_nl. -
index.php on DomainA.nl: The last few lines of the index.php file handle the redirection logic based on the HTTP_HOST value.
The Issue at Hand
The redirect from DomainB.nl to DomainA.nl works perfectly, but it always lands on the default store view (mbv_nl) instead of the specified test_nl store view for that domain.
Diving Deep into the Problem
To solve this issue, we need to understand what’s happening in your index.php file:
switch ($_SERVER['HTTP_HOST']) {
case 'DomainB.nl':
case 'www.DomainB.nl':
$mageRunCode = 'test_nl';
$mageRunType = 'store';
break;
default:
$mageRunCode = '';
$mageRunType = 'store';
break;
}
In this switch-case statement, when the HTTP_HOST matches DomainB.nl or its www variant, it sets $mageRunCode to 'test_nl'. However, even though you’re setting the store view correctly, Magento’s redirection logic seems to override your intention.
The Magento Redirection Logic
Magento has built-in rules for handling domain-based redirects. When a request comes in, it first checks if the HTTP_HOST matches any of the configured domains or the default store view’s domain. If there’s a match but no specific store view is specified (like in your case), it falls back to the default store view.
The Solution Approach
To solve this issue, we’ll need to modify Magento’s behavior slightly. Here are two approaches:
1. Using a Custom Rewrite Rule
You can create a custom rewrite rule in your Magento installation that overrides the default redirection logic for DomainB.nl. This involves creating an observer on the controller_action_predispatch event and manually setting the store view before executing the redirection.
Here’s a basic example of how you might set this up:
class RedirectController extends Controller {
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
// Load desired store view (for DomainB.nl)
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load('test_nl'));
// Perform the redirection
$this->_redirectUrl('/');
}
}
2. Modifying the Core index.php File
Another approach is to modify the core index.php file directly, adding a custom condition before calling Mage::run(). This involves checking if the $mageRunCode is empty and setting it to the desired store view code.
if (empty($mageRunCode)) {
$mageRunCode = 'test_nl';
}
The Best Practice Recommendation
While both solutions work around the issue, I would recommend using the first approachâcreating a custom observer or controller. This method adheres to Magento’s best practice of not modifying core files directly.
Moreover, it allows for better separation of concerns and easier maintenance in the long run. If you need to change the redirection logic or add more conditions in the future, you can do so without touching the core codebase.
Conclusion
Redirecting traffic between domains while maintaining specific store views is a complex task that requires a solid understanding of Magento’s request flow and redirection logic. By using either of the suggested approachesâcustom observers, rewrite rules, or direct modifications to index.phpâyou should be able to solve this issue effectively.
Remember, if you’re working with a live production environment, always ensure you have a backup before making any changes to core files or creating custom event observers.
For more detailed tutorials on Magento development and best practices, I recommend checking out the resources available at Laravel Company. They offer comprehensive guides and tips for working with this powerful e-commerce platform.