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

Issue Type: Bug Created: 2012-03-08T21:40:31.000+0000 Last Updated: 2012-05-20T18:45:04.000+0000 Status: Resolved Fix version(s): Reporter: Bas K (bas) Assignee: Maks 3w (maks3w) Tags: - Zend\Http

  • baseurl

Related issues: - ZF2-211



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/


Posted by Ryan Chouinard (rchouinard) on 2012-03-09T03:29:37.000+0000

This issue also affects deployment via Apache alias directives. I have several ZF1 applications working just fine deployed under 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:

<pre class="literal">
#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.

Posted by David Lukas (dlukas) on 2012-03-16T15:42:26.000+0000

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/'

Posted by Bas K (bas) on 2012-03-16T16:01:13.000+0000

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');

Posted by David Lukas (dlukas) on 2012-04-19T14:00:18.000+0000

To circumvent this problem you can also use the DluBaseUrl: (more info on it here:…). It encapsulates the Bas K's workaround in a simple ZF2 module.

Posted by Maks 3w (maks3w) on 2012-05-19T21:33:19.000+0000

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

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.