3

Read this great reference for creating a csv for import but this was for Magento 1 and it states you only need store_id, id_path,request_path, target_path, options but these headers won't work for Magento 2.

In Magento 2 it looks like the entity_id is required and this would be painful indeed. Does anyone know what fields I need to import to get a massive amount of 301 rewrites into url_rewrites or a table that has paths and entity_id in it so I can cross reference?

Thank you.

5

If you can load your CSV file content then This is the way to import Bulk URL Rewrite:

Add below Code into script than run it.

// MAGENTO START
    include('../../../app/bootstrap.php');

    use Magento\Framework\App\Bootstrap;
    use Magento\Framework\App\Config\ScopeConfigInterface;
    use Magento\Store\Model\StoreManagerInterface;
    use Magento\Framework\Exception\LocalizedException;
    use Magento\UrlRewrite\Model\UrlFinderInterface;
    use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;

    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();

    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');

    $scopeConfig = $objectManager->create('Magento\Framework\App\Config\ScopeConfigInterface');

    $_storeManager = $objectManager->create('Magento\Store\Model\StoreManagerInterface');
    $dataProcessor = $objectManager->create('Magento\Framework\Reflection\DataObjectProcessor');

    $storeId = $_storeManager->getDefaultStoreView()->getStoreId();    

foreach($data as $key => $rec)
{   
    $urlRewrite = $objectManager->create(\Magento\UrlRewrite\Model\UrlRewrite::class);

    $urlRewrite->load(0);

    $model = $urlRewrite;
    $product['entity_type']      = $rec['entity_type'];
    $product['request_path']     = $rec['request_path'];
    $product['target_path']      = $rec['target_path'];
    $product['redirect_type']    = $rec['redirect_type'];
    $product['store_id']         = $rec['store_id'];

    $objectManager->get(\Magento\UrlRewrite\Helper\UrlRewrite::class)->validateRequestPath($product['request_path']);
    $model->setEntityType($product['entity_type'])
        ->setRequestPath($product['request_path'])
        ->setTargetPath($product['target_path'])
        ->setRedirectType($product['redirect_type'])
        ->setStoreId($product['store_id'])
        ->setDescription("Imported Urls");

    $model->save();
}

echo "Successfully Imported!";

$data is the data from your csv or file. which should have same name column as above imported.

entity_id is not required is you choose entity_type "custom"

Set appropriate path to include bootstrap for "include('../../../app/bootstrap.php');", I have added my script in "pub/script/vendor", So I have applied path like this.

|improve this answer|||||
2

I had to do this by exporting the url_rewrite database table then added my custom redirects and imported via phpMyadmin and works fine.

|improve this answer|||||
0

Simple Solution. Just create a csv with two columns

sku, product_websites

Then import it in smaller chunks of about 1000 products each. It should rewrite those product urls.

You can check if the url rewrite worked by going to marketing->url rewrites

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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