skip to Main Content

I’ve changed the SEO Schema settings in the PrestaShop back office (e.g. added .html extension for Product URLs).

Now, the old URLs don’t work anymore and they are not redirect to the new URLs (the ones with .html extension).

I’d like that whenever I change the settings for Products or Categories URLs, the previous URLs are redirected to the new ones (301 permanent redirects).

Any solutions?

2

Answers


  1. For PrestaShop is not important if the URL end with/without .html, the real problem is if you has remove the ID in the route or changed the order of the ID, if this is your case you will need to create a large list of redirections (for each of the categories) in your .htaccess like this example:

    Redirect 301 /3-my-old-category-url /my-new-category-url

    Login or Signup to reply.
  2. By default, PrestaShop does not generate redirect rules based on your changes to handle old URLs (that would be complex to manage, especially in case you make multiples changes in a row).

    You will have to launch a one-time script to generate these permanent redirect rules (301).

    Here’s a example

    The code below assumes that your old URLs were in this format: /category/product, adapt it if needed.

    <?php
    
    include('config/config.inc.php');
    include('init.php');
    
    $context = Context::getContext();
    
    $products = Db::getInstance()->ExecuteS('
    SELECT p.id_product, pl.link_rewrite prod_url, cl.link_rewrite cat_url
    FROM '._DB_PREFIX_.'product p
    LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product)
    LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = p.id_category_default)
    WHERE p.active = 1 AND cl.id_lang = 1 AND cl.id_shop = 1 AND pl.id_lang = 1 AND cl.id_shop = 1');
    
    foreach ($products as $p)
    {
        $new_url = $context->link->getProductLink(new Product((int)$p['id_product']));
        echo 'RewriteRule ^'.$p['cat_url'].'/'.$p['prod_url'].'$ '.$new_url.' [L,R=301]<br />';
    }
    

    Place this script in your PrestaShop root folder and launch it. Then, copy and paste the result into your .htaccess file.

    You can also check this related question:
    PrestaShop – RedirectMatch old product urls (without ID) to new url

    I hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search