symfony: [HttpFoundation] Problem with Request::getBaseUrl and url-rewrites

I’ve encountered an inconsistent behavior with Request::getBaseUrl method when the Request URI does not include the SCRIPT_NAME (due for example to an apache url rewrite), but it includes a segment of the SCRIPT_NAME path.

In RequestTest.php, if I add this data to the getBaseUrlData data provider, testGetBaseUrl fails:

//Test Fails
array(
    '/webmaster',
    array(
        'SCRIPT_FILENAME' => '/foo/bar/web/index.php',
        'SCRIPT_NAME'     => '/web/index.php',
        'PHP_SELF'        => '/web/index.php',
    ),
    '',            //getBaseUrl    Fails, '/web' returned
    '/webmaster',  //getPathInfo   Fails, 'master' returned
)

because in this case getBaseUrl returns ‘/web’ and not ‘’. This make getPathInfo fails too (it returns ‘master’ instead of ‘/webmaster’),

If the URI does not start with ‘web’ all works as expected:

//Test passes
array(
    '/administrator',
    array(
        'SCRIPT_FILENAME' => '/foo/bar/web/index.php',
        'SCRIPT_NAME'     => '/web/index.php',
        'PHP_SELF'        => '/web/index.php',
    ),
    '',          //getBaseUrl
    '/administrator', //getPathInfo
)

About this issue

  • Original URL
  • State: closed
  • Created 14 years ago
  • Comments: 15 (8 by maintainers)

Commits related to this issue

Most upvoted comments

When using mod_rewrite-like functionality, heuristic search for baseUrl can only do so much. It would be better to let developers override baseUrl on a per-request basis, as only they can know the proper value. Allowing to set it in environment configuration would solve this problem.

Below are tests for my specific case, where I rewrote /foo as /foo/public and /foo/bar as /foo/bar/public, equivalent to putting Symfony project in a subdirectory and rewriting url to point to public folder (both tests are equivalent and the latter only represents nesting Symfony project further, such as domain.com/sandbox/project/):

array(
    '/foo/home',
    array(
        'SCRIPT_FILENAME' => '/home/John Doe/public_html/foo/public/app.php',
        'SCRIPT_NAME'     => '/foo/public/app.php',
        'PHP_SELF'        => '/foo/public/app.php',
    ),
    '/foo',        //getBaseUrl     Fails, '' returned
    '/home',       //getPathInfo    Fails, '/foo/home' returned
),
array(
    '/foo/bar/home',
    array(
        'SCRIPT_FILENAME' => '/home/John Doe/public_html/foo/bar/public/app.php',
        'SCRIPT_NAME'     => '/foo/bar/public/app.php',
        'PHP_SELF'        => '/foo/bar/public/app.php',
    ),
    '/foo/bar',      //getBaseUrl     Fails, '' returned
    '/home',         //getPathInfo    Fails, '/foo/bar/home' returned
),