Magento URL Rewrite using config.xml and Regular Expressions

This regular expression can get up to 3 optional variables from a url for your custom Magento extension frontend controller.

$$ /app/code/local/Namespace/Module/etc/config.xml
...
<frontend>
    <routers>
        <[module]>
            <use>standard</use>
            <args>
                <module>Namespace_Module</module>
                <frontName>[module]</frontName>
            </args>
        </[module]>
    </routers>
    <layout>
        <updates>
            <[module]>
                <file>[module].xml</file>
            </[module]>
        </updates>
    </layout>
</frontend>   
...
<global>
    ...
    <rewrite>
        <Namespace_Module>
            <from><![CDATA[#^/[module]/([-\A-Za-z0-9.]+)?/?([-\A-Za-z0-9.]+)?/?([-\A-Za-z0-9.]+)?$#]]></from>
            <to><![CDATA[/[module]/index/index/variable1/$1/variable2/$2/variable3/$3]]></to>
                <complete>1</complete>
        </Namespace_Module>
    </rewrite>
    ...
</global>
...

7 Replies to “Magento URL Rewrite using config.xml and Regular Expressions”

  1. [code]

    [/code]

    my new url is /faq/12/my-identifier-seo-attribute.html
    without .html, i have 404. So it is good for not dupplicate content.

    thanks for your code!

Comments are closed.