URL重写magento自定义模块

时间:2012-09-04 19:42:11

标签: .htaccess magento mod-rewrite url-rewriting rewrite

我正在尝试为我的模块创建一些用户友好的URL。

模块名称正在登陆。现在,我使用索引控制器和索引操作,然后抓取一个字符串" page"从URL抓取我的对象基于此。所以,我的网址如下所示:

http://www.example.com/landing/index/index/page/CoolPage

我目前的想法是将此网址构建为/ landing / {page},以便它只是:

http://www.example.com/landing/CoolPage

首先,我尝试使用htaccess完成此操作。我有以下内容:

RewriteRule ^landing/([a-z\-]+)(/)?$ landing/index/index/page/$1 [R,L]

哪个有效但是重定向而不是重写。我也只用了[L]并且最后没有[]来尝试它,但我最终只能访问我的404页面。

理想情况下,我会使用配置重写,因为它可以与我的模块打包,但我找不到任何关于使用它们的文档。如果按需要工作,我会很高兴使用.htaccess甚至基于数据库的重写。

有没有办法在Magento中进行这样的重写?

2 个答案:

答案 0 :(得分:3)

我想我前一段时间从客户那里得到了完全相同的请求,这就是我管理它的方式,我认为这是最简单最简单的方法......

实际上,可以在模块的config.xml文件中进行重写。 在此示例中,所有网址都是

http://www.domain.com/landing/whatever

将被重写为

http://www.domain.com/landingpage/page/view/whatever

的Config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <My_LandingPage>
            <version>0.0.1</version>
        </My_LandingPage>
    </modules>

    <frontend>
        <routers>
            <landingpage>
                <use>standard</use>
                <args>
                    <module>My_LandingPage</module>
                    <frontName>landingpage</frontName>
                </args>
            </landingpage>
        </routers>
    </frontend>

    <global>
        <!-- Rewrite requested routes -->
        <rewrite>
            <my_landingpage_page_view>
                <from><![CDATA[#^/landing/#]]></from>
                <to>/landingpage/page/view/</to>
                <complete>1</complete>
            </my_landingpage_page_view>
        </rewrite>
</config>

控制器

<?php
class My_LandingPage_PageController extends Mage_Core_Controller_Front_Action {

    /**
     * View page action
     */
    public function viewAction() {

        // Get the requested path (/landing/whatever)
        $pathInfo = $this->getRequest()->getOriginalPathInfo();

        // Extract the requested key (whatever)
        $pathArray = explode('/landing/', $pathInfo);
        $requestedKey = $pathArray[1];

        // So, from there you can use $requestedKey to load any model using it.
        // This is also where you will load and render your layout.

    }
}

关于布局的附注

由于调用的真实控制器操作是“landingpage / page / view”,如果您需要此模块的某些布局,其句柄将为<landingpage_page_view>

答案 1 :(得分:1)

这是一本教科书定制路由器案例,如果我见过的话。您可以采用CMS路由器的方法并调整请求对象上的路径,以便您的控制器可以使用标准路由器进行匹配。

另一种方法是创建一个索引器来为模块的实体创建重写,并将它们存储在core_url_rewrite表中。

相关问题