Magento SEO分页像/ page / 2

时间:2014-01-04 22:01:52

标签: magento pagination seo

我正在尝试使用pager.phtml,基本上我不喜欢magento使用的?p = 2等。
我已经通过这段代码将分页改为我想要的但是这会带来404页面,因为没有/ page / 2等。
我应该使用重写规则吗?有人可以指出我吗?

<div class="pages">
    <strong><?php
    $url=   ($this->getCurrentPage()==1)?
    $this->getPagerUrl(array($this->getOrderVarName()=>$order,  $this->getDirectionVarName()=>$direction,$this->getPageVarName()=>null )).'/':
    $this->getPagerUrl(array($this->getOrderVarName()=>$order,  $this->getDirectionVarName()=>$direction,$this->getPageVarName()=>null )).'/';
     echo $this->__('Page:') ?></strong>
    <ol>
    <?php if (!$this->isFirstPage()): ?>
        <li>
            <a class="previous<?php if(!$this->getAnchorTextForPrevious()): ?> i-previous<?php endif;?>" 
            href="<?php echo $url.'p='.($this->getCurrentPage()-1);?>" title="<?php echo $this->__('Previous') ?>">
                <?php if(!$this->getAnchorTextForPrevious()): ?>
                    <img src="<?php echo $this->getSkinUrl('images/pager_arrow_left.png') ?>" alt="<?php echo $this->__('Previous') ?>" class="v-middle" />
                <?php else: ?>
                    <?php echo $this->getAnchorTextForPrevious() ?>
                <?php endif;?>
            </a>
        </li>
    <?php endif;?>
    <?php
    $pages_Num= ($this->getTotalNum()%$this->getLimit())?(($this->getTotalNum()/$this->getLimit())+1):$this->getTotalNum()/$this->getLimit();
    for($_page=1;$_page<=$pages_Num;$_page++){
        if($this->getCurrentPage()==$_page) echo '<li class="current"><a class="current" href="'.$url.'page/'.$_page.'">'.$_page.'</a></li>';
        else    echo '<li><a href="'.$url.'page/'.$_page.'">'.$_page.'</a></li>';
    }
    ?>
    <?php if (($this->getCurrentPage()+1)<$pages_Num): ?>
        <li><a class="next<?php if(!$this->getAnchorTextForNext()): ?> i-next<?php endif; ?>" 
        href="<?php echo $url.'p='.($this->getCurrentPage()+1);?>" title="<?php echo $this->__('Next') ?>">
                <?php if(!$this->getAnchorTextForNext()): ?>
                    <img src="<?php echo $this->getSkinUrl('images/pager_arrow_right.png') ?>" alt="<?php echo $this->__('Next') ?>" class="v-middle" />
                <?php else: ?>
                    <?php echo $this->getAnchorTextForNext() ?>
                <?php endif;?>
            </a></li>
    <?php endif;?>
    </ol>

</div>

1 个答案:

答案 0 :(得分:0)

首先,您需要创建一个自定义路由器来解析请求的URL。

路由器的方法match()可以用这段代码实现,如下所示:

public function match(Zend_Controller_Request_Http $request) {
    $this->_beforeModuleMatch();

    $url = $request->getRequestUri();

    $suffix = Mage::getStoreConfig('catalog/seo/category_url_suffix');
    $pagerUrlFormat = "\/page\/([0-9]+)";
    if (preg_match('/' . $pagerUrlFormat . preg_quote($suffix, '/') . '/', $url, $match)) {
        $url = str_replace($match[0], $suffix, $url);
        $request->setRequestUri($url);

        $path = $request->getPathInfo();
        $path = str_replace($match[0], $suffix, $path);
        $request->setPathInfo($path);
        $request->setParam('p', $match[1]);

        return true;
    }

    return false;
}

此代码将生成此类网址:http://example.com/category/p/2.html

NB!请注意,上面的代码尚未经过测试。它只是我们的SEO模块中使用的更高级代码的简化版本(SEO Suite Ultimate

以下是执行您实际需要的功能的说明:

  

能够为分页页面创建寻呼机URL格式示例:

     

-page [page_number]会将网址转换为/mobile-phones-page2.html / p / [page_number]会将网址转换为/mobile-phones/p/2.html