Friendly URL problem in front module - Prestashop

扶醉桌前 提交于 2020-06-29 06:41:27

问题


I am building a front module for a website that is using 301 Moved Permanently option in SEO and URLs configuration. Wesbite uses Prestashop 1.6.1.9.

In module, I am defining the route like this:

public static $ModuleRoutes = array(
    'module-aacategories-viewmapping-mapping' => array(
        'controller' => 'viewmapping',
        'rule' => 'mappings{/:tree}',
        'keywords' => array(
            'tree' =>        array('regexp' => '[/_a-zA-Z0-9-\pL]*', 'param' => 'tree'),
        ),
        'params' => array(
            'fc' => 'module',
            'module' => 'aacategories',
        )
    )
);

In browser address bar, when I enter:

site.local/en/mappings/test-map/first-test

I get:

Please use the following URL instead:

site.local/en/index.php?controller=viewmapping&tree=test-map%2Ffirst-test&module=aacategories

This latter link gives 404. However, when I append &fc=module to the url, it goes to desired page.

The problems:

1- How to force Prestashop routing to append &fc=module at the end?

2- How to keep the friendly url in address bar and not be redirected?

Note: When I change configuration in SEO and URLs to no redirection, then it works. But it is not the configuration needed in prod.

Your help is much appreciated. Thanks in advance.


回答1:


The problem is you are setting public property $php_self in your module controller.

You need to remove the property so that core front controller does not do a canonical redirect.

The code that does this is in FrontController.php line 378.

if (!empty($this->page_name)) {
    $page_name = $this->page_name;
} elseif (!empty($this->php_self)) {
    $page_name = $this->php_self;
} elseif (Tools::getValue('fc') == 'module' && $module_name != '' && (Module::getInstanceByName($module_name) instanceof PaymentModule)) {
    $page_name = 'module-payment-submit';
}
// @retrocompatibility Are we in a module ?
elseif (preg_match('#^'.preg_quote($this->context->shop->physical_uri, '#').'modules/([a-zA-Z0-9_-]+?)/(.*)$#', $_SERVER['REQUEST_URI'], $m)) {
    $page_name = 'module-'.$m[1].'-'.str_replace(array('.php', '/'), array('', '-'), $m[2]);
} else {
    $page_name = Dispatcher::getInstance()->getController();
    $page_name = (preg_match('/^[0-9]/', $page_name) ? 'page_'.$page_name : $page_name);
}

And then does a canonical redirect if you set that property on line 401.

if (!empty($this->php_self) && !Tools::getValue('ajax')) {
    $this->canonicalRedirection($this->context->link->getPageLink($this->php_self, $this->ssl, $this->context->language->id));
}


来源:https://stackoverflow.com/questions/62458761/friendly-url-problem-in-front-module-prestashop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!