2

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?

2

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.

  • Liyakat - thank you so much for your suggestion. It seems really cool but honestly - I don't understand it...I only get by by following step by step instructions - do this, then this, then this - then you are done! :-) I can also install extension manually by replacing files and changing contents of files if I know what to replace with what. I am learning a lot though so maybe some time in the future I be able to know how to do what you are suggesting....Thank you again - I appreciate you, :-* Allysin – Allysin May 2 '15 at 12:02
  • @Allysin,Glad i could suggest you best you. At least votup answer if someone gives you correct way to do.. – liyakat May 2 '15 at 12:12
  • I'm really sorry! I'm gonna sound real dumb now - how do I vote up in here? It helps you if I vote for you? – Allysin May 2 '15 at 17:34
  • you can click on up Arrow left side of my answer. – liyakat May 3 '15 at 5:14
2

Another possible solution would be to add a javascript redirect inside the html of you 404 page This can be done from the backend CMS.

Login to the Admin and go to CMS->Pages->Manage Content, and select your 404 page (the url key of this page would be something like "no-route")

click on the show/hide editor button to hide the text editor and append the following code to your 404 page, between tags.


<script type="text/javascript">
function leave() {
  window.location = "{{store url=''}}";
}
setTimeout("leave()", 5000);
</script>

The 5000 specifies the delay in miliseconds after which to redirect the visitor.

The advantage of this method is that it can be rolled back easily, and implemented quite fast.

0

This doesn't seem like a Magento specific issue.

Depending on the number of redirects I would advise that the redirects are either completed at htaccess level or vhost level.

You should also use 301 redirects otherwise you will harm your SEO. Also unless you have no real alternative I would redirect to the homepage as the last resort.

Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html
  • SEO is what this is all about. However, I don't really know which links are going to be 'broken' and which ones might remain the same. I had an old Magento store (1.3.1) with about 1300 products which I am now copying BY HAND to the new Magento 1.9.1. database (including the same URL-key)! Is there any way to tell which links I need to setup? I assume I will hard code the old URL and the new redirect URL in the .htaccess file for the 301 redirect? Thanks! :-* – Allysin May 2 '15 at 22:05
  • You could let Google web master tools do the job for you as it will tell you which pages 404. Or you could use programs like screaming frog which will also do the same job. – Goose84 May 3 '15 at 5:28
0

Simple way of doing this,login to the admin, go to System>Configuration>Web and change the drop-down value for CMS No Route Page in the Default Pages section. You should be able to select your CMS Home Page.

0

I have redirected the 404 page into home page.

Go to admin panel Admin -> System -> Configuration -> Web -> Default Pages -> CMS No Route Page and select "Home page" from drop-down.

-1

create folders “Mage\Core\Controller\Varien\Router\” in yours “app\code\local\” folder copy source file “Default.php” from “app\code\core\Mage\Core\Controller\Varien\Router\” into your local folder update the line 42:

$actionName = isset($noRoute[1]) ? $noRoute[1] : 'index';

– and replace with:

$actionName = isset($noRoute[2]) ? $noRoute[2] : 'index';

Enjoy coding.

  • Rapid - thank you for helping. Just to make sure - you say: 1. Create this: Mage\Core\Controller\Varien\Router\ under “app\code\local\” 2. I will copy “Default.php” from “app\code\core\Mage\Core\Controller\Varien\Router\” into my local folder? I'm really sorry - which local folder are we talking about? Then 3. change the code in the “Default.php” file? Thanks again! :-* – Allysin May 2 '15 at 12:03

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.