2

My Magento store experiences a high amount of products that get added and then deleted. I have been asked to implement a 410 status code on these deleted pages but I am unsure how.

Looking at magento's core code i can see it handles 404's like so in the core indexController:

/**
 * Default index action (with 404 Not Found headers)
 * Used if default page don't configure or available
 *
 */
public function defaultIndexAction()
{
    $this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
    $this->getResponse()->setHeader('Status','404 File not found');

    $this->loadLayout();
    $this->renderLayout();
}

/**
 * Render CMS 404 Not found page
 *
 * @param string $coreRoute
 */
public function noRouteAction($coreRoute = null)
{
    $this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
    $this->getResponse()->setHeader('Status','404 File not found');

    $pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_NO_ROUTE_PAGE);
    if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
        $this->_forward('defaultNoRoute');
    }
}

/**
 * Default no route page action
 * Used if no route page don't configure or available
 *
 */
public function defaultNoRouteAction()
{
    $this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
    $this->getResponse()->setHeader('Status','404 File not found');

    $this->loadLayout();
    $this->renderLayout();
}

Is there any way I can add a function to a deleted page to send a 410 header?

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
odd_duck
  • 1,396
  • 4
  • 28
  • 47

1 Answers1

1

When products are deleted, they are gone from the database. Since you probably don't want to replace every 404 response with a 410 response, you will need to track URLs of deleted products.

Possible approach:

  • observe the catalog_product_delete_before event and store the path of all URL rewrites for the product that is going to be deleted as property in the observer
  • observe catalog_product_delete_after to recreate the now deleted URL rewrites for the saved paths, but as custom rewrites to a new controller action, you could call it missingproduct
  • In this controller action, send the 410 header similar to noRouteAction and show a nice error page
Fabian Schmengler
  • 65,791
  • 25
  • 187
  • 421
  • I've almost got this working, i observe the catalog_product_delete_before event and in my custom module class i have the function storeDeletedUrl. Inside this function how do I get the url path of the product to be deleted? – odd_duck Sep 07 '15 at 10:46
  • I don't quite understand: what is storeDeletedUrl supposed to do and where do you call it? – Fabian Schmengler Sep 07 '15 at 10:50
  • I've created a custom module DeletedUrlsSave and in my config.xml i have <events><catalog_category_delete_before><observers><company_deletedurlssave><type>model</type><class>Company_DeletedUrlsSave_Model_DeletedUrlsSave</class><method>storeDeletedUrl</method>…. so i observe the event and call the storeDeletedUrl function in my class Company_DeletedUrlsSave_Model_DeletedUrlsSave – odd_duck Sep 07 '15 at 10:58
  • Ah OK, so this is your observer method. Try $product_id = $observer->getProduct()->getId(); $rewriteCollection = Mage::getResourceModel('core/url_rewrite_collection')->addFieldToFilter('id_path', array('regexp' => "^product/{$product_id}(\$|/)")); to get a collection of all rewrites for this product. You can loop over it and call getRequestPath() on each item to get the URLs – Fabian Schmengler Sep 07 '15 at 11:13