You need to override the redirect() method in vendor/magento/module-url-rewrite and add logic to check for the params and then re-append them to the target url that the redirect goes to.
<?php
namespace YouNameSpace\YourModule\Controller\Plugin;
use \Magento\UrlRewrite\Controller\Router as MagentoRouter;
use Magento\UrlRewrite\Model\UrlFinderInterface;
class Router extends MagentoRouter
{
public function __construct(
\Magento\Framework\App\ActionFactory $actionFactory,
\Magento\Framework\UrlInterface $url,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResponseInterface $response,
UrlFinderInterface $urlFinder
)
{
parent::__construct($actionFactory, $url, $storeManager, $response, $urlFinder);
}
/**
* @param \Magento\Framework\App\RequestInterface $request
* @param string $url
* @param int $code
* @return \Magento\Framework\App\ActionInterface
*/
protected function redirect($request, $url, $code)
{
if(!empty($request->getParams())){
$urlParams = $this->formatParams($request->getParams());
if($urlParams != false){
$url = $url.$urlParams;
}
}
$this->response->setRedirect($url, $code);
$request->setDispatched(true);
return $this->actionFactory->create('Magento\Framework\App\Action\Redirect');
}
/**
* @param $params
* @return bool|string
*/
public function formatParams($params){
$paramsArray = [];
foreach($params as $k => $v){
$paramsArray[] = $k.'='.$v;
}
if(!empty($paramsArray)){
$paramstring = '?'.implode('&',$paramsArray);
return $paramstring;
}
return false;
}
}
I initially went with the plugin approach on this but soon realized that the method was protected so the only way to get at it is a class override in you modules di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\UrlRewrite\Controller\Router" type="YouNameSpace\YourModule\Controller\Plugin\Router"/>
</config>
It should be possible to modify the url in the same class via a plugin by hooking into the public match() method but I was fine with using a preference for the class override.