Issues

ZF-2602: Zend_Controller_Action::_redirect() does not work correctly with empty or '/' base

Description

There is a problem with Zend_Controller_Action::_redirect(), which proxies to Zend_Controller_Action_Helper_Redirector::gotoUrl():

Sample: Application URI: http://www.example.com Application base path: either empty or / Current URI: http://www.example.com/foo

Controller-Command:


$this->_redirect('bar');

Expected target URI: http://www.example.com/bar

What currently happens: http://www.example.com/foo/bar

However, if the application path is a subdirectory, everything works fine.

The problem is related to the code in the Zend_Controller_Action_Helper_Redirector::_prependBase() function:


    /**
     * Determine if the baseUrl should be prepended, and prepend if necessary
     *
     * @param  string $url
     * @return string
     */
    protected function _prependBase($url)
    {
        if ($this->getPrependBase()) {
            $request = $this->getRequest();
            if ($request instanceof Zend_Controller_Request_Http) {
                $base = rtrim($request->getBaseUrl(), '/');
                if (!empty($base) && ('/' != $base)) {
                    $url = $base . '/' . ltrim($url, '/');
                }
            }
        }

        return $url;
    }

The problem can easily be fixed by adding two lines:


    /**
     * Determine if the baseUrl should be prepended, and prepend if necessary
     *
     * @param  string $url
     * @return string
     */
    protected function _prependBase($url)
    {
        if ($this->getPrependBase()) {
            $request = $this->getRequest();
            if ($request instanceof Zend_Controller_Request_Http) {
                $base = rtrim($request->getBaseUrl(), '/');
                if (!empty($base) && ('/' != $base)) {
                    $url = $base . '/' . ltrim($url, '/');
                } else { 
                    $url = '/' . ltrim($url, '/');
                }
            }
        }

        return $url;
    }

Comments

Scheduling for 1.5.0RC2

Fixed in trunk and release branch, with corresponding unit test.