3

I have a client with a pre-existing Magento site in production mode. When the site was created the client was not informed of the importance of the url keys attribute in that he had to make it SEO-friendly. At this point all the site urls are using a number format e.g. /107 instead of something like /toys. Categories and subcategories are ok to change manually because, the aren't that many but having 1000+ products makes it impossible or at least too time consuming.

Are there any scripts out there to change the url-keys to the product name? e.g. /108 into green-power-ranger assuming the initial product name was GREEN POWER RNAGER

3 Answers 3

0

Anyone know of a tested script to regenerate url-keys?

<?php  
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
ini_set('memory_limit', '600M');
ini_set('max_execution_time', 1800);
umask(0);
Mage::app('admin'); 
   $products = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable'); 
    $productModel = Mage::getModel('catalog/product');
    foreach ($products as $product) {         
            $productNameLowercase = strtolower($product->getName());
            $productKey = clean($productNameLowercase);             
            $productUpdate = $productModel->load($product->getId());
            $productUpdate->setUrlKey($productKey);
            $productUpdate->save();     

    }
    function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
2
0

You can use this extension to correct the URL keys and re-generate them in a massive way based on product names, it also adds the UTF-8 URL Key support for your store.

www.magentocommerce.com/magento-connect/url-key-master-by-magendev.html

there is also a video here that shows how it dose that.

0

Magento doesn't do this that easily.

However, if you truncate the core_url_rewrite table and re index. It will fix your issues if you have bad URLs. Here is a tested php script. Then re index ssh preferred method for lots of prodcuts.

<?php
// Create connection
        $con=mysqli_connect("localhost","username","password","dbName");

// Check connection
        if (mysqli_connect_errno($con))
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }    

    $sql = "TRUNCATE TABLE core_url_rewrite";
    mysqli_query($con, $sql);
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.