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)
Links to this issue
Commits related to this issue
- [HttpFoundation] fix detection of request uri, base url, base path, path info I synchronized the code with the changes done upstream in ZF: https://github.com/zendframework/zf2/blob/master/library/Ze... — committed to Tobion/symfony by Tobion 14 years ago
- bug #13039 [HttpFoundation] [Request] fix baseUrl parsing to fix wrong path_info (rk3rn3r) This PR was squashed before being merged into the 2.3 branch (closes #13039). Discussion ---------- [HttpF... — committed to symfony/symfony by fabpot 12 years ago
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/):