Error 404 Page – How to Redirect Product Error 404 to Catalog Search

404-pageerror

I want to redirect every 404 product to catalog search because I have anothers products with similar name.

I have a lot of products are more them 1000 products with 404, that dont exist any more, but those old link are published in another websites and users still came from that links, so to keep the user on my website I want to redirect this broke link to catalog search, NOTE: "cant do it on magento rewrite url product by product because those product dont exist anymore"

I am thinking to do some route on source code to do some like this:

OLD link: http://www.mywebsite.com/iphone-5-16GB.html -> broke link

REDIRECT TO: http://www.mywebsite.com/catalogsearch/result/?q=iphone+5+16gb

I cant find any solution here on stackoverflow and nothing on Google results.

How can I do that?

Best Answer

You can do this by override ProductController.php (Mage_Catalog_ProductController) and on view action put cod for redirection. to catalog searcg page.

How to override a controller,please check here.

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

and put some code viewAction function:

  ..... 
   try {
            $viewHelper->prepareAndRender($productId, $this, $params);
        } catch (Exception $e) {
            if ($e->getCode() == $viewHelper->ERR_NO_PRODUCT_LOADED) {
                if (isset($_GET['store'])  && !$this->getResponse()->isRedirect()) {

                 //   $this->_redirect('');
        $this->_redirect('catalogsearch/result/',array('q'=>'$quertString));
        return;
                } elseif (!$this->getResponse()->isRedirect()) {
                   // $this->_forward('noRoute');
        $this->_redirect('catalogsearch/result/',array('q'=>'$quertString));
        return;

                }
            } else {
                Mage::logException($e);
        $this->_redirect('catalogsearch/result/',array('q'=>'$quertString));
        return;

              //  $this->_forward('noRoute');
            }
        }
Related Topic