I've written an extension for Magento which will initiate a 404 redirect from the controller under certain circumstances. The code in the controller that causes the 404 redirect is:
$this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
$this->getResponse()->setHeader('Status','404 File not found');
$pageId = Mage::getStoreConfig('web/default/cms_no_route');
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('defaultNoRoute');
}
This code is accessed via the URL www.mysite.com/mymodule/index/action?name=value and the redirect is triggered based on 'value'.
However, because because my module contains a layout update which overwrites the root node:
<layout version="0.1.0">
<mynamespace_mymodule_index_action>
<block type="mymodule/list" name="root" template="mynamespace/mymodule/list.phtml" output="toHtml" />
</mynamespace_mymodule_index_action>
</layout>
the 404 redirect is showing a blank (white) page, which contains the following HTML:
<head>
</head>
<body>
<div id="outerdivshade">
<div id="outerdivpadding">
<div class="wrapper">
<div class="page">
<div class="main-container col2-left-layout">
<div class="main">
<div class="col-main"></div>
<div class="col-left sidebar"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
I'm guessing that by updating of the root node I've left a layout structure that the redirect code cannot work with to create the complete 404 page.
By removing the if statement and leaving just the code:
$this->_forward('defaultNoRoute');
everything works, but I don't understand the pros and cons of doing this.
Interestingly the above code works correctly on Magento 1.6, but not on 1.7.
I'd appreciate any comments on the right way of including a working 404 redirect, but still allowing me to use my own root node in the layout.
Thanks
For showing the 404 page you can call the norouteAction
of the Mage_Core_Controller_Varien_Action
class.
$this->norouteAction();
return;
The 404 page can be edited in the backend. The page has the title 404 Not Found 1
.
You can also put in there your custom layout update XML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With