5
votes

I'm running a Magento Community website (on version 1.5.1) and I'm having an issue with "404" pages.

We have users landing on our site from direct links and also google/bing search results. The pages they go to may not be correct as they may have changed. Magento uses MVC to route requests to the correct controller, but when there isn't a controller Magento displays a static CMS page (i.e. "404" page). The problem is that this page doesn't allow me to write custom PHP code on it so I can't log the URL that caused the 404.

If I can find the right point in the code prior to displaying the CMS 404 page then I can log the URL and use that to do the appropriate URL rewriting.

Can someone help me as to where the code is that finally gives up on any controller and displays the custom CMS "404" page?

2

2 Answers

5
votes

Basically this is happening in Mage_Cms_IndexController::noRouteAction(). But you also could just have a look at your webservers log for entries with the 404 return code (404 is set in the same method).

2
votes

Alex was spot-on with his answer. I thought I'd post the code I wrote to solve the problem based on his answer.

/**
 * Render CMS 404 Not found page
 *
 * @param string $coreRoute
 */
public function noRouteAction($coreRoute = null)
{
    $this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
    $this->getResponse()->setHeader('Status','404 File not found');

    /* JCS */
    $path = $this->getRequest()->getPathInfo();

    if (!startsWith($path, '/media/')) {
        if (!startsWith($path, '/images/')) {
            if (!startsWith($path, '/ebay/')) {
                if (!startsWith($path, '/app/')) {
                    Mage::log('JCS:noRouteAction:path:'.$path);
                }
            }
        }
    }

    $pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_NO_ROUTE_PAGE);
    if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
        $this->_forward('defaultNoRoute');
    }
}

I also added a startsWith function:

/* JCS */
function startsWith($haystack, $needle)
{
    $length = strlen($needle);
    return (substr($haystack, 0, $length) === $needle);
}