在ZF2中,我知道可以创建一个301重定向,并附加一个查询字符串,如下所示:
$options = [
'query' => [
'string' => 'hello world',
]
];
return $this->redirect()
->toRoute('myRoute', [], $options)
->setStatusCode(301);
但是,这将重定向到带有hello%20world
附加的URL 。在ZF2中,有没有一种方法可以编写带有hello+world
附加内容的重定向?
由于ZF2不提供使用urlencode
而不是使用查询字符串进行重定向的本机函数rawurlencode
,因此我们编写了一个自定义方法来进行重定向。不太漂亮,但现在解决了我们的问题:
private function redirectToPageFive($query)
{
$location = ($_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://')
. $_SERVER['HTTP_HOST']
. '/search?query='
. urlencode($query)
. '&page=5';
header("Location: $location", true, 301);
exit;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句