我创建了一个模块并尝试使用config.xml
重写url我的网址:http://www.domain.com/mymodule/index/bestseller
我希望网址为:
http://www.domain.com/bestseller
以下是我的xml代码:
<rewrite>
<products_rewrite>
<from><![CDATA[/\/(.*)/]]></from>
<to><![CDATA[mymodule/index/$1/]]></to>
<complete>1</complete>
</products_rewrite>
</rewrite>
此config.xml网址规则有效,但它打破了所有其他网址,网站中的所有其他网址都返回404错误。有人可以帮助我吗?
答案 0 :(得分:2)
您可以在模块中为其创建自定义路由器。
将其添加到config.xml
代码
<global>
<events>
<controller_front_init_routers>
<observers>
<[namespace]_[module]>
<class>[Namespace]_[Module]_Controller_Router</class>
<method>initControllerRouters</method>
</[namespace]_[module]>
</observers>
</controller_front_init_routers>
</events>
现在您需要创建路由器类。
app/code/local/[Namespace]/[Module]/Controller/Router.php
中的输入以下代码:(注意:文件夹名称为Controller
- 请勿与controllers
混淆。)
<?php
class [Namespace]_[Module]_Controller_Router
extends Mage_Core_Controller_Varien_Router_Abstract {
public function initControllerRouters($observer){
$front = $observer->getEvent()->getFront();
$front->addRouter('[namspace]_[module]', $this);
return $this;
}
public function match(Zend_Controller_Request_Http $request){
if (!Mage::isInstalled()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect(Mage::getUrl('install'))
->sendResponse();
exit;
}
$urlKey = trim($request->getPathInfo(), '/');
$parts = explode('/', $urlKey);
if ($parts[0] == 'bestseller'){ //if the route matches 'bestseller' then internal redirect to the module
$request->setModuleName('[modulename]') //set module name
->setControllerName('index') //set controller
->setActionName('bestseller'); //set action
$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$urlKey
);
return true;
}
if ($parts[0] == 'special'){ //if the route matches 'special' then internal redirect to the module
$request->setModuleName('[modulename]') //set module name
->setControllerName('index') //set controller
->setActionName('special'); //set action
$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$urlKey
);
return true;
}
return false;
}
}
清除缓存并开始使用。
答案 1 :(得分:1)
我最终使用这样的分隔重写:
<rewrite>
<rewrite_latest>
<from><![CDATA[#^/latest.html#]]></from>
<to><![CDATA[/mymodule/index/latest]]></to>
<complete>1</complete>
</rewrite_latest>
<rewrite_popular>
<from><![CDATA[#^/popular.html#]]></from>
<to><![CDATA[/mymodule/index/popular]]></to>
<complete>1</complete>
</rewrite_popular>
<rewrite_special>
<from><![CDATA[#^/special.html#]]></from>
<to><![CDATA[/mymodule/index/special]]></to>
<complete>1</complete>
</rewrite_special>
<rewrite_bestseller>
<from><![CDATA[#^/bestseller.html#]]></from>
<to><![CDATA[/mymodule/index/bestseller]]></to>
<complete>1</complete>
</rewrite_bestseller>
<rewrite_featured>
<from><![CDATA[#^/featured.html#]]></from>
<to><![CDATA[/mymodule/index/featured]]></to>
<complete>1</complete>
</rewrite_featured>
</rewrite>
它有效,但看起来不太好,如果有人有更好的解决方案,请告诉我。
答案 2 :(得分:-1)
来自config.xml的代码
<global>
<rewrite>
<designer_url>
<from><![CDATA[#^/author/id/$#]]></from>
<to><![CDATA[/designer/index/index/id/$1]]></to>
<complete>1</complete>
</designer_url>
</rewrite>
</global>