Magento - 为所有产品添加301重定向(约6000种产品)

I want to add product category in the URL of the product and add 301 redirect from old URL (without category) to new URL (with category) for a Magento site. There are nearly 6000 products with 30 different categories, which makes it impossible to add all redirects in .htaccess.

Here is and example for what I'm trying to do-
OLD URL looks like - http://domainname.com/product-name.html
NEW URL will look like - http://domainname.com/category/product-name.html

It's easy to configure the URL, but I want to add 301 redirect for SEO purpose.
So that, if anyone hits old URL - http://domainname.com/product-name.html in search results, then he should get redirected to http://domainname.com/category/product-name.html

Thanks.

You can write custom script and you can add custom rewrite magento

  1. Get all product collection programmatically (Magento - get product collection of all products)
  2. Add magento core/url_rewrite table (https://gist.github.com/byjujohn/5508233)