【问题标题】:Redirect 404 error to custom 404 page将 404 错误重定向到自定义 404 页面
【发布时间】:2015-08-01 19:09:20
【问题描述】:

我在 Zend Framework 1 项目中的 404 页面有问题。

如果出现路由、操作或控制器错误,我的应用程序会返回 404 错误。唯一的问题是我希望我的链接更改为 www.exemple.com/error404.html 之类的内容。所以假设我有这个链接:

http://www.exemple.com/some/something/dfsf

我的应用程序返回 404 错误,但链接保持不变。我希望我的链接从上面重定向到:

http://www.exemple.com/error404

在我的 ErrorController 中,我有以下代码:

public function errorAction()
{
    $errors = $this->_getParam('error_handler');

    switch ($errors->type) {
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

            // 404 error -- controller or action not found

            $this->getResponse()->setHttpResponseCode(404);
            $this->view->message = 'Page not found';

            break;

    }
    $this->view->exception = $errors->exception;
    $this->view->request   = $errors->request;
}

我尝试重定向到我创建的页面,但重定向代码是 302,我需要 404。

   public function errorAction()
{
    $errors = $this->_getParam('error_handler');

    switch ($errors->type) {
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

            // 404 error -- controller or action not found
            $this->getResponse()->setHttpResponseCode(404);
            $this->view->message = 'Page not found';

            //redirect to a 404 page
            $this->_redirect('/error404');
            break;

    }
    $this->view->exception = $errors->exception;
    $this->view->request   = $errors->request;
}

谢谢!

【问题讨论】:

    标签: redirect zend-framework routes http-status-code-404


    【解决方案1】:

    404 也不能是重定向,两者是互斥的。如果您只想渲染error404 模板,请从错误控制器执行此操作(或修改现有的错误操作模板)。

    另请参阅 Google 关于软 404 的建议:https://support.google.com/webmasters/answer/181708

    【讨论】:

    • 实际上我已经更改了我的 404 页面模板内容,我的问题是我需要它的链接是这样的 www.exemple.com/404.html。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2016-01-06
    • 2023-03-27
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2015-04-17
    相关资源
    最近更新 更多