Magento – How to forward all the 404 Magento pages to the front page

404-pagece-1.9.1.0url

I have a simple question…I had an old store and people bookmarked specific product pages in their browsers as well as Google search points to some specific pages. I have now installed a new version of Magento (1.9.1) and some of these URLs will not longer work (they will show the standard 404 not found Magento page).

What would be the best way to forward all these hits to the front page?

Best Answer

As you said only for product pages then it would be better to override only product view controller

here i can give you little example to create your own extension to make this sense

config.xml

<frontend> 
        <routers> 
          <catalog> 
             <args> 
             <modules> 
                <Allysin_OldProductsHandler before="Mage_Catalog">Allysin_OldProductsHandler</Allysin_OldProductsHandler>
             </modules> 
             </args> 
         </catalog> 
       </routers> 
   </frontend>    

i was done with the child products when they are now not visible to catalog search and redirect customer to parent products. But you may change logic for controller to set your own redirect as you like

And in ProductController.php

<?php
include_once "Mage" . DS . "Catalog" . DS . "controllers" . DS . "ProductController.php";

class Allysin_OldProductsHandler_ProductController extends Mage_Catalog_ProductController {

    protected function _construct() {

        $this->_read = Mage::getSingleton("core/resource")->getConnection("core_read"); 
    }
    public function viewAction() {
        // Get initial data from request
        $categoryId = (int) $this->getRequest()->getParam('category', false);
        $productId = (int) $this->getRequest()->getParam('id');
        $specifyOptions = $this->getRequest()->getParam('options');

        // Prepare helper and params
        $viewHelper = Mage::helper('catalog/product_view');

        $params = new Varien_Object();
        $params->setCategoryId($categoryId);
        $params->setSpecifyOptions($specifyOptions);

        // Render page
        try {
            $viewHelper->prepareAndRender($productId, $this, $params);
        } catch (Exception $e) {
            if ($e->getCode() == $viewHelper->ERR_NO_PRODUCT_LOADED) {

                $hasParent = $this->getParentData($productId);

// if current product has parent then redirect to parent products
                if($hasParent['hasParent']){
                    return Mage::app()->getResponse()->setRedirect($hasParent['redirectUrl'],301);
                } elseif (!$this->getResponse()->isRedirect()) {
                    $this->_forward('noRoute');
                }
            } else {
                Mage::logException($e);
                $this->_forward('noRoute');
            }
        }
    }

    protected function _getConfigurableAttribute(){

        $select =  $this->_read->select()->from(array("a" => "eav_attribute"), 
                                                array("attribute_code", "frontend_label"))  
                                                ->join(array("cea"=>"catalog_eav_attribute"),"a.attribute_id = cea.attribute_id",array())  
                                                ->join(array("ao" => "eav_attribute_option"), "a.attribute_id = ao.attribute_id", array())
                                                ->where('cea.is_global = ?', '1')
                                                ->where('cea.is_configurable = ?', '1')

                                                ->order("frontend_label");                                                 

        $data =  $this->_read->fetchPairs($select); 
        return count($data) ? $data : array();
    }

    protected function getParentData($productId) {

        $parent_id = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($productId);

        if (!empty($parent_id)) {
            $redirect = '';
            $parent_product = Mage::getModel('catalog/product')->load($parent_id[0]);
            $attributes = $parent_product->getTypeInstance(true)->getConfigurableAttributes($parent_product);

            $attributeCodes = $this->_getConfigurableAttribute();
            $attributeCodes =  array_flip($attributeCodes);

            foreach ($attributes as $attribute) {
                $productAttribute = $attribute->getProductAttribute();

                if (in_array($productAttribute->getAttributeCode(),$attributeCodes)) {
                    /* getting only 'in stock' child products */
                    $allProducts = $parent_product->getTypeInstance(true)->getUsedProducts(null, $parent_product);
                    foreach ($allProducts as $each_product) {
                        if ($each_product->getId() == $productId) {

                            $valueText = $each_product->getData($productAttribute->getAttributeCode());
                            $att_code = $productAttribute->getId();
                            $parent_product->setStoreId($storeId);
                            $url_parent = $parent_product->getProductUrl();
                            $redirect = $url_parent . "#" . $att_code . "=" . $valueText;
                        }
                    }
                }
            }

            return array('hasParent' => 1,
                         'redirectUrl' => $redirect);
        }
        return array('hasParent' => 0);
    }
}

And module configuration file will looks like

Allysin_OldProductsHandler.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Allysin_OldProductsHandler>
            <active>true</active>
            <codePool>local</codePool>
        </Allysin_OldProductsHandler>
    </modules>
</config>

i am sure you have better idea now.

hope this will work for You.

Related Topic