My old site had tens of thousands of no-longer-valid products with urls like http://example.com?product_id=123456.

They are all search-engine indexed and now resolve to 404 in Magento. I cannot possibly create a redirect for every single one.

How do I get them to resolve to my home page?

  • 123456 is product id? – Amit Bera May 8 '15 at 6:51
  • @AmitBera yes it's an example of the product id. – Joe Palaca May 9 '15 at 3:58
  • please check, i have update answer – Amit Bera May 9 '15 at 6:41
  • You cannot possibly? I have a client with 500,000 301 redirects entered. You could possibly; you're choosing not to. BTW redirects shouldn't live on forever. They should be terminated within 6 months. – philwinkle Dec 15 '15 at 16:46

In this case,you can use event/observer and on event controller_front_init_before fire an observer.

First i guess that your old product id and new product id are same.

On that observer check query string($_SERVER['QUERY_STRING']) content

?product_id=productID pattern in url then basic of this redirect to current system product url.



Old system product url: http://example.com?product_id=123456

New system product url: http://example.com/abx.html

my code will be redirect http://example.com?product_id=123456 to http://example.com/abx.html using 301 redirection.

301 redirect, you will resolved SEO issue.

Here config.xml code:

      <controller_front_init_routers> <!-- identifier of the event we want to catch -->
          <controller_front_init_before_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento67130/observer</class> <!-- observers class alias -->
            <method>redirectionProduct</method>  <!-- observer's method to be called -->

Observer.php code is below:

class StackExchange_Magento67130_Model_Observer

    public function redirectionProduct(Varien_Event_Observer $observer)

                // Check query string exits in product_id
            if(strpos($this->_getQueryString(),'product_id=')!== false){
                /* get Product ifd from query string */
                $product= Mage::getModel('catalog/product')->load($productid);
                 if ($product->getId()) {
                    echo $product->getProductUrl();
                        ->setRedirect($product->getProductUrl(), 301)



    /* Check here Request query string */
    protected function _getQueryString()
            if (!empty($_SERVER['QUERY_STRING'])) {
                $queryParams = array();
                parse_str($_SERVER['QUERY_STRING'], $queryParams);
                $hasChanges = false;
                foreach ($queryParams as $key => $value) {
                    if (substr($key, 0, 3) === '___') {
                        $hasChanges = true;
                if ($hasChanges) {
                    return http_build_query($queryParams);
                } else {
                    return $_SERVER['QUERY_STRING'];
            return false;
| improve this answer | |

you have to create one little custom extension to redirect if product is not found in your database

here i have given working solution and its perfect worked for me

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

hope this will also work for you.

| improve this answer | |

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.