zf2 重定向查询字符串包含 + 而不是 %20
zf2 redirect with query string containing + instead of %20
在 ZF2 中,我知道我可以创建一个 301 重定向并附加如下查询字符串:
$options = [
'query' => [
'string' => 'hello world',
]
];
return $this->redirect()
->toRoute('myRoute', [], $options)
->setStatusCode(301);
但是,这会重定向到附加了 hello%20world
的 URL。在 ZF2 中,有没有一种方法可以用附加 hello+world
来编写此重定向?
您的 url 正常 url_encode
d。它与 URL.
的有效字符一样多 "transformed"
在接收端,您的 "hello%20world"
将被自动接收为 "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;
}
在 ZF2 中,我知道我可以创建一个 301 重定向并附加如下查询字符串:
$options = [
'query' => [
'string' => 'hello world',
]
];
return $this->redirect()
->toRoute('myRoute', [], $options)
->setStatusCode(301);
但是,这会重定向到附加了 hello%20world
的 URL。在 ZF2 中,有没有一种方法可以用附加 hello+world
来编写此重定向?
您的 url 正常 url_encode
d。它与 URL.
在接收端,您的 "hello%20world"
将被自动接收为 "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;
}