Skip to content

Commit 007c799

Browse files
author
Ibrahim BinAlshikh
committed
fix(routing): preserve query string on redirect and prevent crash on root sub-routes
- Router::redirect() now forwards query string parameters from the original request to the redirect target. Fixes #303. - fixUriPath() now checks strlen before accessing $path[0] in the while loop, preventing 'Uninitialized string offset 0' when grouping sub-routes under root path '/'. Fixes #302.
1 parent 9c5c3b6 commit 007c799

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

WebFiori/Framework/Router/Router.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ public static function redirect(string $path, string $to, int $code = 301) {
607607
if (!in_array($httpCode, $allowedCodes)) {
608608
$httpCode = 301;
609609
}
610+
$requestedUri = Router::getRouteUri()->getRequestedUri();
611+
if ($requestedUri !== null && strlen($requestedUri->getQueryString()) > 0) {
612+
$to .= '?'.$requestedUri->getQueryString();
613+
}
610614
App::getResponse()->addHeader('location', $to);
611615
App::getResponse()->setCode($httpCode);
612616

@@ -1104,7 +1108,7 @@ private function fixFilePath($path) {
11041108
private function fixUriPath(string $path): string {
11051109
if (strlen($path) != 0 && $path != '/') {
11061110
if ($path[strlen($path) - 1] == '/' || $path[0] == '/') {
1107-
while ($path[0] == '/' || $path[strlen($path) - 1] == '/') {
1111+
while (strlen($path) > 0 && ($path[0] == '/' || $path[strlen($path) - 1] == '/')) {
11081112
$path = trim($path, '/');
11091113
}
11101114
$path = '/'.$path;

0 commit comments

Comments
 (0)