How to Redirect /checkout/cart Page in Magento

cartcheckoutonestepcheckout

I've combined my checkout page (OneStepCheckout) and cart page. The /checkout/cart (the cart page) is still accessible, and I wouldn't want my customer to accidentally access that page (reviewing your cart twice would be quite confusing).

Is there anyway I can redirect this page with magento to /checkout ?

Best Answer

In this case you need 301 redirection from checkout/cart to your checkout page

  Mage::app()->getResponse()
        ->setRedirect($someUrl, 301)
        ->sendResponse();

use event controller_action_predispatch_checkout_cart_index fire an observer

<frontend>
    <events>
        <controller_action_predispatch_checkout_cart_index>
            <observers>
                <customRedir>
                    <class>YourModulePrefix/observer</class>
                    <method>customRedir</method>
                </customRedir>
            </observers>
        </controller_action_predispatch_checkout_cart_index>
        </events>
</frontend>

Observer code:

<?php
class MymoduleNameSpace_ModuleName_Model_Observer{

    public function customRedir($oberver){
          Mage::app()->getResponse()
        ->setRedirect($someUrl, 301)
        ->sendResponse();
        die();

    }
}

Full Module:

As per as your request, i have out full module and as per as customer will redirect to checkout/cart to contacts us using 301 redirection.

config code:

location: app\code\local\Stackexchange\Magento67150\etc\config.xml

code:

<?xml version="1.0"?>
<config>
  <modules>
    <StackExchange_Magento67150>
      <version>0.1.0</version>
    </StackExchange_Magento67150>
  </modules>
  <global>
    <models>
      <magento67150>
        <class>StackExchange_Magento67150_Model</class>
        <resourceModel>magento67150_mysql4</resourceModel>
      </magento67150>
    </models>
    <events>
      <controller_action_predispatch_checkout_cart_index> <!-- identifier of the event we want to catch -->
        <observers>
          <controller_action_predispatch_checkout_cart_index_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento67150/observer</class> <!-- observers class alias -->
            <method>customRedir</method>  <!-- observer's method to be called -->
             </controller_action_predispatch_checkout_cart_index_handler>
        </observers>
      </controller_action_predispatch_checkout_cart_index>
    </events>
  </global>
</config> 

Observer code:

location: app\code\local\Stackexchange\Magento67150\Model\Observer.php

Code:

<?php
class StackExchange_Magento67150_Model_Observer
{

            public function customRedir(Varien_Event_Observer $observer)
            {

                $controller = $observer->getControllerAction();
                $controller->getResponse()->setRedirect(Mage::getUrl('contacts'),301)
                ->sendResponse();
                return;
            }

}

Module file: app/etc/modules/StackExchange_Magento67150.xml

code is:

<?xml version="1.0"?>
<config>
  <modules>
    <StackExchange_Magento67150>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </StackExchange_Magento67150>
  </modules>
</config>