Tag Archives: Magento Migration

Bulk Additions of URL Re-Writes in Magento

In moving a site that is already established into the magento e-commerce cart one major challenge is to populate URL rewrites that will re-direct all the old product URL’s into the new magento SEO URL’s

There are a number of ways you can do this, below is an explanation of a very simplistic re-map, by mapping all the old URL’s to the root of the new site, now this is not ideal for a number of reasons firstly that any external links to products will be remapped down the root of the new site, not the product they used to point to, but even this is better than thousands of 404 errors.

The first task is to create a database table with all the old URL’s in, you have a number of options here, you can either extract them via SQL or some other form from your old cart, ideally leaving off the domain name and http string, if this is not possible then you can easily remove this later either with search and replace in the file or within MySQL itself as below:

update core_url_rewrite
set id_path = right(id_path, length(id_path) – 26),
request_path = right(request_path, length(request_path) – 26)
where id_path like 'http://www.mydomain.co.uk/%'

The value 26 used in the SQL above is the number of characters you want to remove from the front of the URL’s this is quick and dirty,  and could be done dynamically but this is a one hit job so there is very little point.  The SQL above is targeted to be used against the Magento core_url_rewrite table after the URL’s have been added but could easily be used against any table either before or after loading. next you need to load these URL’s into a database table on the target MySQL server.

Having done this the job is relatively simple.

The first thing to do is ensure that none of the URL’s you are about to load into the mg_core_rewrite table are duplicates of anything that is already there. the table I have loaded my URL’s into for this example is called ‘newurls’ and has a single field ‘url’

first we need to get rid of any duplication against mg_core_rewrite

DELETE from newurls WHERE url in (SELECT id_path FROM core_url_rewrite

The SQL above will delete from our table anything which is  a duplicate and already exists in the core_url_rewrite table, next we can add our new URL’s to the table and point them to the root of the domain.

INSERT INTO core_url_rewrite (store_id, id_path, request_path, target_path, is_system, options)
SELECT DISTINCT 1, url, url, 'http://www.mydomain.co.uk/', 0, ‘RP’
FROM `newurls`

This SQL will load full records into the core_url_rewrite table of Magento and point the URL’s to the root of the domain.

Simples, no more 404 errors from your old URL’s, now to do this and map the products back to each other you are going to need the SKU of the products loaded with the URL’s in your original SQL extract data, I will cover this in another article at a later date.