Skip to content

Commit 4343fe4

Browse files
committed
fix: fixed the codeigniter routing for installations in subdirectories with the page index.
I fixed the codeigniter routing for installations in subdirectories by removing the path to the subdirectory from the uri during the routing process in the SiteURIFactory. I am still testing the change as it's in a very sensible area of the code. I also still need to write tests for this change. I will do that soon.
1 parent b65b1f5 commit 4343fe4

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

system/HTTP/SiteURIFactory.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,32 @@ public function detectRoutePath(string $protocol = ''): string
111111
*/
112112
private function parseRequestURI(): string
113113
{
114+
$appConfig = config(\Config\App::class);
115+
$baseUrl = $appConfig->baseURL;
116+
$indexPage = $appConfig->indexPage;
117+
$baseUri = false;
118+
$parsedUrl = parse_url($baseUrl);
119+
120+
if(isset($parsedUrl['path'])){ // The path could be empty if the url is just a domain
121+
$baseUri = $parsedUrl['path'];
122+
}
123+
if($baseUri){
124+
$baseUriArray = explode('/', $baseUri);
125+
$baseUriArray = array_filter($baseUriArray); // We remove the empty strings from the array
126+
$baseUri = implode('/', $baseUriArray); // We join the array back into a string with slashes
127+
if(strlen($baseUri) > 0){
128+
$baseUri = "/" . $baseUri; // We add a slash at the beginning of the base Uri as implode will not do that
129+
}else{
130+
$baseUri = false;
131+
}
132+
}
133+
134+
$serverRequestUri = $this->superglobals->server('REQUEST_URI'); // We get the request URI from the server superglobals
135+
if($baseUri) $serverRequestUri = ltrim($serverRequestUri, $baseUri); // We remove the base Uri from the request URI if it exists, baseUri is the path to the subdirectory
136+
if($indexPage) $serverRequestUri = ltrim($serverRequestUri, "/" . $indexPage); // We remove the index page from the request URI if it exists
137+
114138
if (
115-
$this->superglobals->server('REQUEST_URI') === null
139+
$serverRequestUri === null
116140
|| $this->superglobals->server('SCRIPT_NAME') === null
117141
) {
118142
return '';
@@ -122,7 +146,7 @@ private function parseRequestURI(): string
122146
// string contains a colon followed by a number. So we attach a dummy
123147
// host since REQUEST_URI does not include the host. This allows us to
124148
// parse out the query string and path.
125-
$parts = parse_url('http://dummy' . $this->superglobals->server('REQUEST_URI'));
149+
$parts = parse_url('http://dummy' . $serverRequestUri);
126150
$query = $parts['query'] ?? '';
127151
$path = $parts['path'] ?? '';
128152

0 commit comments

Comments
 (0)