Search code examples
prestashopprestashop-1.6

Module route with rewrite redirect to product on Prestahop


I created a custom route for an artist page:

public function hookModuleRoutes($params)
{
    return [
        'module-artists-artist' => [
            'controller' => 'artist',
            'rule' => 'artists/{id_artist}',
            'keywords' => [
                'id_artist' => ['regexp' => '[0-9]+', 'param' => 'id_artist'],
            ],
            'params' => [
                'fc' => 'module',
                'module' => 'artists',
                'controller' => 'artist'
            ],
        ],
    ];
}

If I test with /artists/1, this works. But I want to add the link_rewrite property. So I modified the configuration like this:

public function hookModuleRoutes($params)
{
    return [
        'module-artists-artist' => [
            'controller' => 'artist',
            'rule' => 'artists/{id_artist}-{rewrite}',
            'keywords' => [
                'id_artist' => ['regexp' => '[0-9]+', 'param' => 'id_artist'],
                'rewrite' => ['regexp' => '[_a-zA-Z0-9\pL\pS-]*'],
            ],
            'params' => [
                'fc' => 'module',
                'module' => 'artists',
                'controller' => 'artist'
            ],
        ],
    ];
}

But when I try /artists/1-baxter, I'm redirected to the product page of product with ID 1. My artist controller is never called.

[Debug] This page has moved
Please use the following URL instead: http://localhost:8000/fr/estampes/1-est-ce-que-etre

How can I solve it?


Solution

  • This is because URLs generated by your pattern also match the product URL pattern, which has higherprecedence. PrestaShop doesn't check whether product exists, it just redirects directly to ProductController. Page patterns in PrestaShop differ from each other so that URLs can be quickly recognized to be associated with X controller. You can confirm this by checking the default patterns.

    You can check the product URL pattern in back-office: SEO & URLs or in DispatcherCore class. Anyway, if you want an easy fix I'd suggest making this pattern:

    artists/{id_artist}/{rewrite}