ZF2-206: baseUrl incorrect when installation is within a user's document directory


The baseUrl calculated in Zend\Http\PhpEnvironment\HttpRequest::detectBaseUrl() does not take into account installations within a document root of a user.

example: http://localhost/~user/some/path/to/installation/

I expect baseUrl to be : /~user/some/path/to/installation/


This issue also affects deployment via Apache alias directives. I have several ZF1 applications working just fine deployed under http://domain.com/app-name/. Attempting to do the same with ZF2 fails to properly detect the baseUrl.

A deployment of the ZF2 Skeleton Application to http://localhost/zf2-skeleton/ fails with the following stack trace:

#0 /var/www/html/zf2-skeleton/library/Zend/Di/Di.php(130): Zend\Di\Di->newInstance('zf2-skeleton', Array)
#1 /var/www/html/zf2-skeleton/library/Zend/Mvc/Application.php(332): Zend\Di\Di->get('zf2-skeleton')
#2 [internal function]: Zend\Mvc\Application->dispatch(Object(Zend\Mvc\MvcEvent))
#3 /var/www/html/zf2-skeleton/library/Zend/EventManager/EventManager.php(441): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#4 /var/www/html/zf2-skeleton/library/Zend/EventManager/EventManager.php(202): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#5 /var/www/html/zf2-skeleton/library/Zend/Mvc/Application.php(251): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 /var/www/html/zf2-skeleton/public/index.php(28): Zend\Mvc\Application->run()
#7 {main}

Converting the alias to a vhost and using http://zf2-skeleton.local/ works just fine.

The classname mentioned in the description should read Zend\Http\PhpEnvironment\Request. I experience the same problem, ie it is impossible to deploy the ZF2 application into any virtual server subdirectory (alias). It works only when deployed into a vhost root.

My environment: - Windows 7 - ZendServer CE 5.6.0 - PHP 5.3.9 - Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/0.9.8o - ZF2 v2.0.0beta3

httpd.conf: Alias /zf2tut "c:/WebDev/PhpStorm/ZF2Tutorial/public" Options FollowSymLinks Order Allow,Deny Allow from All AllowOverride All

.htaccess RewriteEngine On RewriteBase /zf2tut/ RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.$ - [NC,L] RewriteRule ^.$ index.php [NC,L]

Zend\Http\PhpEnvironment\Request::detectBaseUrl() debug: SCRIPT_FILENAME = 'C:/WebDev/PhpStorm/ZF2Tutorial/public/index.php' SCRIPT_NAME = '/zf2tut/index.php' PHP_SELF = '/zf2tut/index.php' ORIG_SCRIPT_NAME = null return value = '' (on line 329), should be '/zf2tut/'

A workaround is to set the base url in the index.php


or when using the ZendFrameworkSkeleton Application you can set base url when instantiating the view in the module...

public function initializeView($e) { $app = $e->getParam('application');

    $basePath     = $app->getRequest()->getBasePath();
    $locator      = $app->getLocator();
    $renderer     = $locator->get('Zend\View\Renderer\PhpRenderer');

To circumvent this problem you can also use the DluBaseUrl: https://bitbucket.org/dlu/dlubaseurl (more info on it here: http://zfdaily.com/2012/04/…). It encapsulates the Bas K's workaround in a simple ZF2 module.

I created the following PR for fix this issue. Could you test the fix?