Magento 1.9 – How to Create Custom 404 Error Page

layoutmagento-1.9template

According to this article http://alanstorm.com/magentos_many_404_pages/
there is three 404 pages in Magento:
1 The one dispatched by the apache
2 CMS page
3 Generated via layout XML

If I need to create my own distinct 404 page, that has different design from default Magento 404, the best way is probably 'Adding custom Layout XML Updates to the handles cms_index_noroute and cms_index_defaultnoroute', right?

What I did:

1 New module

2 <frontend>
<layout>
<updates>
<mine_eshop>
<file>mine/eshop.xml</file>
</mine_eshop>
</updates>
</layout>
</frontend>

3

<cms_index_defaultnoroute>
    <remove name="right"/>
    <remove name="left"/>

    <reference name="content">
        <block type="core/template" name="mine_no_route" template="mine/404.phtml"/>
    </reference>
</cms_index_defaultnoroute>

And now I got my custom template right below the default one. And this is inside default Magento page layout.

There was no 404 CMS page configured or found.

New 404

How to display only the custom 404.phtml on the page in case of that error?
They say there is no 'right' way to do it and it is pretty frustrating. What are the best practices of creating a custom error pages?

Best Answer

You can change it via configuration but if you want to replace the template file then I would do it via local.xml instead.

Create local.xml if you don't have in your theme's layout folder and add this:

<?xml version="1.0"?>
<layout version="0.1.0">
    <cms_index_defaultnoroute>
     <reference name="default_no_route">
         <action method="setTemplate">
             <template>your/path/to/no-route-file.phtml</template>
         </action>
     </reference>
    </cms_index_defaultnoroute>
</layout>

This will replace the template for no route AKA 404 page. I haven't tested but should work. Good luck.