Blog
 

(Mass) Redirect Module for Magento

(Mass) Redirect Module for Magento

UPDATE: Please notice that this extension is deprecated and properlly wont work within the latest Magento versions. It has been only tested within Magento 1.3. (April/2012)

One of the things you have to deal with when switching from another e-commerce software to Magento are URL redirects. Over the past years you (hopefully) received many incoming deep links. You surely know that incoming links are one of the main ranking factors for most search engines. So when you switch to another e-commerce software, these deep links get useless because they point to subpages that don’t exist (unless you don’t have the exact same URL structure in your new shop). The common way to solve this issue is just to do a 301 redirect (permantly redirect) from the old URLs to the new ones. You have the possibility to define the redirects in your .htaccess file or with the Magentos build-in “URL Rewrite Management” (in the Backend “Catalog” -> “URL Rewrite Management”).

However, both of these methods have some disadvantages. The .htaccess file could be overwritten when you update Magento. Also if you change the URL structure inside Magento you’ll have to update the .htaccess file each time. The Magento build-in Tool is nice if you only have to do a couple of rewrites. But let’s say you have 2000 or more redirects you want to define… you can take a week off, because you have to define each redirect manually.

I decided to write a small extension to give you an alternative way of defining redirects. Just named it “Massredirect”. You can download it here. For installation just copy it into your Magento main folder and run the command “tar -xvf massredirect.tar”. This will extract the extension files to the desired places. Before you start defining the redirects, make sure you got rid of the cache and the extension shows up in the advanced tab of the configuration site. To define the redirects open the CSV file “redirects.csv” located in the folder “/app/code/local/Velite/Massredirect/etc/” in a text editor. Each row in the CSV defines a redirect. Basically there are two possibilties of defining a redirect:

Possbility 1:

old-URL;type;entity_id;website_id;store_id

for example:

www.myshop.com/product.php?productid=123;product;5;1;1

This will redirect a user typing the URL “www.myshop.com/product.php?productid=123″ to the product page of the product with the ID 5 on the default website and store. Notice that the URL must be specified without “http://”. “type” can either be “product” or “category”.

Possbility 2:

old-URL;new-URL

for example:

www.myshop.com/product.php?productid=123;www.myshop.com/my-nice-product.html

Keep in mind that the second possbility has the same disadvantage like defining the redirects in the .htaccess file. This means, if you change the URL structure inside of Magento you will also have to update the CSV. However, you can mix both possibilities in the CSV file.

So how does this extension work?

Basically the preDispatch method of the Mage_Core_Controller_Varien_Action gets observed (the event is named “controller_action_predispatch”). If the requested action is “noRoute”, the CSV gets parsed and looked up if there is a mapping available for the typed-in URL. In that case the user (or the bot) gets redirected to the new URL (with a Status 301-Redirect).

Feel free to make any further suggestions.