【问题标题】:zf2 redirect with query string containing + instead of %20zf2 使用包含 + 而不是 %20 的查询字符串重定向
【发布时间】:2017-01-11 08:27:57
【问题描述】:

在 ZF2 中,我知道我可以创建一个 301 重定向,并附加如下查询字符串:

$options = [ 
    'query' => [
        'string' => 'hello world', 
    ]
];

return $this->redirect()
            ->toRoute('myRoute', [], $options)
            ->setStatusCode(301);

但是,这会重定向到附加了 hello%20world 的 URL。在 ZF2 中,有没有办法编写这个重定向并附加 hello+world 代替?

【问题讨论】:

    标签: php redirect zend-framework zend-framework2


    【解决方案1】:

    由于 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;
    }
    

    【讨论】:

      【解决方案2】:

      您的网址是正确的url_encoded。它被“转换”为 URL 的有效字符。

      在接收端,您的"hello%20world" 将被自动接收为"hello world"

      没有什么可修复的,代码按预期工作。

      【讨论】:

      • +0 实际上,字符串不是urlencoded。它是rawurlencoded,结果是%20,而不是首选的+。每个搜索查询都需要一个唯一的 URL。因此我们需要重定向到hello+world 而不是hello%20world。现有的代码可能会按照最初的预期执行,但我们需要选择urlencode 而不是rawurlencode
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2016-12-24
      • 2013-05-07
      • 2021-05-20
      相关资源
      最近更新 更多