Skip to content

Commit a32ab17

Browse files
committed
Ensure base_url has trailing slash to work for those who are hosting in subfolders.
1 parent c20b7d7 commit a32ab17

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

app/Config/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class App extends BaseConfig
2121
| environments.
2222
|
2323
*/
24-
public $baseURL = 'http://localhost:8080';
24+
public $baseURL = 'http://localhost:8080/';
2525

2626
/*
2727
|--------------------------------------------------------------------------

system/Helpers/url_helper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ function base_url($uri = '', string $protocol = null): string
111111
// otherwise get rid of the path, because we have
112112
// no way of knowing the intent...
113113
$config = \CodeIgniter\Config\Services::request()->config;
114-
$url = new \CodeIgniter\HTTP\URI($config->baseURL);
114+
115+
// If baseUrl does not have a trailing slash it won't resolve
116+
// correctly for users hosting in a subfolder.
117+
$baseUrl = ! empty($config->baseURL) && $config->baseURL !== '/'
118+
? rtrim($config->baseURL, '/ ') . '/'
119+
: $config->baseURL;
120+
121+
$url = new \CodeIgniter\HTTP\URI($baseUrl);
115122
unset($config);
116123

117124
// Merge in the path set by the user, if any

tests/system/Helpers/URLHelperTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,23 @@ public function testBaseURLWithSegmentsAgain()
316316
$this->assertEquals('http://example.com/profile', base_url('profile'));
317317
}
318318

319+
public function testBaseURLHasSubfolder()
320+
{
321+
$_SERVER['HTTP_HOST'] = 'example.com';
322+
$_SERVER['REQUEST_URI'] = '/test';
323+
324+
// Since we're on a CLI, we must provide our own URI
325+
$config = new App();
326+
$config->baseURL = 'http://example.com/subfolder';
327+
$request = Services::request($config, false);
328+
$request->uri = new URI('http://example.com/subfolder/test');
329+
330+
Services::injectMock('request', $request);
331+
332+
$this->assertEquals('http://example.com/subfolder/foo', base_url('foo'));
333+
$this->assertEquals('http://example.com/subfolder/', base_url());
334+
}
335+
319336
//--------------------------------------------------------------------
320337
// Test current_url
321338

0 commit comments

Comments
 (0)