2014-04-08 37 views
-1

我創建了一個模塊,並嘗試使用config.xml中Magento的config.xml中的URL重寫

重寫URL

我的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 url規則的作品,但它破碎了所有其他的你rl,網站中的所有其他網址都會返回404錯誤。有誰能夠幫助我?

回答

2

您可以爲您的模塊創建一個自定義路由器。

這給​​3210添加<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> 

現在,您需要創建路由器類中。

in 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; 
    } 
} 

清除緩存,並給它一個去。

+0

我還有一個問題,其實我有服務器的URL需要重寫,例如,MyModule的/指數/暢銷書,MyModule的/指數/特殊,MyModule的/索引/最新的,mymodule/index/featured等。我該怎麼做? – user1579524

+0

使用上面相同的代碼,只需在'$ parts [0]'上做一個'switch'語句,根據它的值,你可以在內部重定向到正確的動作 – Marius

+0

我已經編輯了答案,並添加了另外一種情況, url密鑰是'special'。你可以做同樣的最新和功能 – Marius

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> 

它的工作原理,但看起來不是很好,如果有人有更好的解決方案,請讓我知道。

-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>