【问题标题】:Why does Zend Framework's $this->_redirect($url, array('code'=>301)) result in a 302 code?为什么 Zend Framework 的 $this->_redirect($url, array('code'=>301)) 会导致 302 代码?
【发布时间】:2012-02-16 17:01:50
【问题描述】:

当我打电话时:

$this->_redirect($url, array('code'=>301));

我被重定向了,但 http 代码是 302 而不是 301。知道为什么吗?

【问题讨论】:

  • 我刚刚对此进行了测试,它对我来说效果很好(ZF 1.11)。你用的是什么版本?

标签: zend-framework redirect http-status-code-301


【解决方案1】:

_redirect($url, array $options = array()):重定向到另一个位置。此方法接受一个 URL 和一组可选的选项。经过 默认情况下,它执行 HTTP 302 重定向。

尝试使用以下方法将重定向代码设置为 301:

$this->_helpers->redirector->setCode(301);

【讨论】:

    【解决方案2】:

    使用 _redirect 方法似乎总是会将代码设置为 302。如果您想手动更改响应代码,请改用 redirector

    【讨论】:

    • $this->_redirector = $this->_helper->getHelper('Redirector'); $this->_redirector->setCode(301);让它工作
    【解决方案3】:

    在 Zend/Controller/Action/Helper/Redirector.php: _redirect 调用 $this->getCode(); getCode() 是 $this->_code 的简单 getter; _code 初始化为 302。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 2021-01-30
      • 2011-07-22
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多