Redirect 404 error to custom 404 page

I have a problem with my 404 page in a Zend Framework 1 project.

In case of route, action or controller error my application return 404 error. The only problem is that I want my link to change in something like www.exemple.com/error404.html. So let's say I have this link:

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

My application return 404 error, but the link remain the same. I would like that my link from above to redirect to :

http://www.exemple.com/error404

In my ErrorController I have the following code:

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

I try to do a redirect to a page created by me, but the redirect code is 302 and I need 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;
}

Thank you!

1 Answers
  1. A 404 cannot also be a redirect, the two are mutually exclusive. If all you want to do is render your error404 template, do this from the error controller (or modify the existing error action template).

    See also Google's recommendations on soft 404s: https://support.google.com/webmasters/answer/181708

    2015-05-20 15:52:50
Related Articles
You Might Also Like