Take a look at how Magento generates it's sitemap.xml
file.
Specifically the Mage_Sitemap_Model_Sitemap
class method generateXml
.
For example, following snippet generates them for the categories, I've rewritten it a little to export to a flat text file.
$ioWrite = new Varien_Io_File();
$path = '';
$ioWrite->open(array('path' => $path));
$ioWrite->streamOpen($path.'urls.txt');
$storeId = Mage::app()->getStore()->getId();
$date = Mage::getSingleton('core/date')->gmtDate('Y-m-d');
$baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
/**
* Generate categories sitemap
*/
$changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
$priority = (string)Mage::getStoreConfig('sitemap/category/priority', $storeId);
$collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
$categories = new Varien_Object();
$categories->setItems($collection);
Mage::dispatchEvent('sitemap_categories_generating_before', array(
'collection' => $categories
));
foreach ($categories->getItems() as $item) {
$ioWrite->streamWrite(htmlspecialchars($baseUrl . $item->getUrl()));
}
unset($collection);
$ioWrite->close();
Add the code from the class to your own script to generate a text file
It turns out the Redirect directive in an .htaccess file won't match against the query string, only the request uri (the part before the question mark). To match against the query string you'll need to use the RewriteCond and RewriteRule directives. More info on this here: https://simonecarletti.com/blog/2009/01/apache-query-string-redirects/
This should work in the .htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=331$
RewriteRule ^index\.php$ http://gearx.local/catalogsearch/result?q=sku-331 [R=301,L]
If all the urls you need to redirect are in the same format with a numeric id then these two lines should take care of them all:
RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=([0-9]+)$
RewriteRule ^index\.php$ http://gearx.local/catalogsearch/result?q=sku-%1 [R=301,L]
I'd recommend redirecting to the actual url of the product though rather than a search results page.
Best Answer
If you are worried about SEO, you should use the Canonical URL option. You could find it under: System -> Configuration -> Catalog -> Search Engine Optimization -> Use Canonical Link Meta Tag For Categories & Use Canonical Link Meta Tag For Products.