这种行为的原因可以在 中找到Mage_Core_Model_Url_Rewrite::rewrite
。没有商店代码的基本 URL 没有重定向。
下面是一个非常丑陋的解决方案,但它应该适用于您的情况。只要在请求 URI 中找不到当前商店代码,它将重定向到包含商店代码的基本 URL:
应用程序/代码/本地/Danslo/RedirectToStore/Model/Observer.php:
<?php
class Danslo_RedirectToStore_Model_Observer
{
public function redirectToStore($observer)
{
$request = $observer->getFront()->getRequest();
$storeCode = Mage::app()->getStore()->getCode();
$requestUri = $request->getRequestUri();
if (strpos($requestUri, $storeCode) === false) {
$targetUrl = $request->getBaseUrl() . '/' . $storeCode;
header('HTTP/1.1 301 Moved Permanently');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Location: ' . $targetUrl);
exit;
}
}
}
应用程序/代码/本地/Danslo/RedirectToStore/etc/config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<global>
<events>
<controller_front_init_before>
<observers>
<redirect_to_store>
<class>Danslo_RedirectToStore_Model_Observer</class>
<method>redirectToStore</method>
<type>singleton</type>
</redirect_to_store>
</observers>
</controller_front_init_before>
</events>
</global>
</config>
应用程序/etc/modules/Danslo_RedirectToStore.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Danslo_RedirectToStore>
<active>true</active>
<codePool>local</codePool>
</Danslo_RedirectToStore>
</modules>
</config>