1

I'm trying to rewrite an URL to a CMS page and append some GET parameters.

My CMS page has the code "foobar" and can be accessed via "/foobar". On the CMS page i have some custom logic which uses GET parameters, e.g. "/foobar?test=123". This domain can be called without problems.

Now i want to make that accessible via "/foobar_123" and tried it with the rule:

RewriteRule ^foobar_([0-9]+)$ /foobar?test=$1 [L]

which isn't working (not found). I guess it's because of Magento looking for an CMS page called "foobar?test=123" ?

I also tried defining the rewrite in my config.xml, like that:

<rewrite>
    <foobar_test> 
        <from><![CDATA[#^/foobar_([0-9]+)#]]></from>
        <to><![CDATA[/foobar?test=$1]]></to>
    </foobar_test>           
</rewrite>

Which isn't working as well (leads to an 404).

How can i archieve the wanted behaviour?

Thx

1 Answer 1

1

Let's assume you have a structure like foobar/test/123 where foobar is your CMS page identifier. By default, Magento will try to match that whole string to a CMS page identifier and when it fails you will be presented with a 404 page. You can go around that by modifying match method found in Mage_Cms_Controller_Router class.

First extract CMS page identifier:

$params = explode('/', $identifier);
$identifier = $params[0];

If the match is found we iterate over passed parameters and set them on request object

for ($i = 1, $l = sizeof($params); $i < $l; $i += 2) {
        $request->setParam($params[$i], isset($params[$i+1]) ? urldecode($params[$i+1]) : '');
    }

Putting it all together:

public function match(Zend_Controller_Request_Http $request)
{
    if (!Mage::isInstalled()) {
        Mage::app()->getFrontController()->getResponse()
            ->setRedirect(Mage::getUrl('install'))
            ->sendResponse();
        exit;
    }

    $identifier = trim($request->getPathInfo(), '/');

    $condition = new Varien_Object(array(
        'identifier' => $identifier,
        'continue'   => true
    ));
    Mage::dispatchEvent('cms_controller_router_match_before', array(
        'router'    => $this,
        'condition' => $condition
    ));
    $identifier = $condition->getIdentifier();

    if ($condition->getRedirectUrl()) {
        Mage::app()->getFrontController()->getResponse()
            ->setRedirect($condition->getRedirectUrl())
            ->sendResponse();
        $request->setDispatched(true);
        return true;
    }

    if (!$condition->getContinue()) {
        return false;
    }

    # extract page identifier
    $params = explode('/', $identifier);
    $identifier = $params[0];

    $page   = Mage::getModel('cms/page');
    $pageId = $page->checkIdentifier($identifier, Mage::app()->getStore()->getId());
    if (!$pageId) {
        return false;
    }

    # set params on request object
    for ($i = 1, $l = sizeof($params); $i < $l; $i += 2) {
        $request->setParam($params[$i], isset($params[$i+1]) ? urldecode($params[$i+1]) : '');
    }

    $request->setModuleName('cms')
        ->setControllerName('page')
        ->setActionName('view')
        ->setParam('page_id', $pageId);
    $request->setAlias(
        Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
        $identifier
    );

    return true;
}
1
  • Thanks a lot, that was really helpful! :) Just for completeness, what i did was copying the file "/app/code/core/Mage/Cms/Controller/Router.php" to "/app/code/local/Mage/Cms/Controller/Router.php" and made the changes there to be update-safe.
    – kaimitku
    Jan 18, 2015 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.