Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用magento2将尾部斜杠添加到url中_Php_Magento - Fatal编程技术网

Php 使用magento2将尾部斜杠添加到url中

Php 使用magento2将尾部斜杠添加到url中,php,magento,Php,Magento,如何在没有指定文件(301重定向)的情况下使用magento2添加url的尾部斜杠,而不使用mod_rewrite,仅在代码中。app/code/your_vendor/your_module/etc/frontend/events.xml app/code/your_vendor/your_module/Observer/CustomPredispatch.php <?xml version="1.0"?> <config xmlns:xsi="http://www.w

如何在没有指定文件(301重定向)的情况下使用magento2添加url的尾部斜杠,而不使用mod_rewrite,仅在代码中。

app/code/your_vendor/your_module/etc/frontend/events.xml

app/code/your_vendor/your_module/Observer/CustomPredispatch.php

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch_cms_index_index">
        <observer name="unique_name" instance="your_vendor\your_module\Observer\CustomPredispatch" />
    </event>
</config>
<?php

namespace your_vendor\your_module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class CustomPredispatch implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $request = $observer->getEvent()->getRequest();
        if(substr($request->getRequestUri(), -1) !== '/'){
            $observer->getEvent()->getControllerAction()->getResponse()->setRedirect($request->getRequestUri() . '/', 301)->sendResponse();
        }
    }

}