Skip to content

Commit 966c26e

Browse files
committed
fix: corrected Apache redirect loop
1 parent ab51013 commit 966c26e

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

phpmyfaq/.htaccess

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,23 @@ Header set Access-Control-Allow-Headers "Content-Type, Authorization"
144144
RewriteRule ^services/webauthn(.*) index.php [L,QSA]
145145
# User pages
146146
RewriteRule ^user/(ucp|bookmarks|request-removal|logout|register) index.php?action=$1 [L,QSA]
147-
# Setup and update pages
148-
RewriteCond %{REQUEST_URI} ^(.*/)setup/
149-
RewriteRule ^(.*/)?setup/(.*) $1setup/index.php [L,QSA]
150-
RewriteRule ^update/(.*) update/index.php [L,QSA]
151147
# Administration API
152148
RewriteRule ^admin/api/(.*) admin/api/index.php [L,QSA]
153149
# Administration pages
154150
RewriteRule ^admin/(.*) admin/index.php [L,QSA]
155151
# Private APIs
152+
RewriteCond %{REQUEST_URI} !index\.php$
156153
RewriteRule ^api/(autocomplete|bookmark/delete|bookmark/create|user/data/update|user/password/update|user/request-removal|user/remove-twofactor|contact|voting|register|captcha|share|comment/create|faq/create|question/create|webauthn/prepare|webauthn/register|webauthn/prepare-login|webauthn/login|translations) api/index.php [L,QSA]
157154
# Setup APIs
155+
RewriteCond %{REQUEST_URI} !index\.php$
158156
RewriteRule ^api/setup/(check|backup|update-database) api/index.php [L,QSA]
159157
# REST API v3.0 and v3.1
160158
# * http://[...]/api/v3.x/<ACTION>
159+
RewriteCond %{REQUEST_URI} !index\.php$
161160
RewriteRule ^api/v3\.[01]/(.*) api/index.php [L,QSA]
161+
# Setup and update pages
162+
RewriteCond %{REQUEST_URI} ^(.*/)setup/
163+
RewriteCond %{REQUEST_URI} !index\.php$
164+
RewriteRule ^(.*/)?setup/(.*) $1setup/index.php [L,QSA]
165+
RewriteRule ^update/(.*) update/index.php [L,QSA]
162166
</IfModule>

phpmyfaq/src/Bootstrap.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,17 @@
9191

9292
//
9393
// Check if config/database.php exist -> if not, redirect to the installer
94+
// Skip redirect if we're already in setup or API setup context
9495
//
96+
$requestUri = $_SERVER['REQUEST_URI'] ?? '';
97+
$isSetupContext = str_contains($requestUri, '/setup/') || str_contains($requestUri, '/api/setup/');
98+
9599
if (!file_exists(PMF_CONFIG_DIR . '/database.php') && !file_exists(PMF_LEGACY_CONFIG_DIR . '/database.php')) {
96-
$response = new RedirectResponse('./setup/');
97-
$response->send();
100+
if (!$isSetupContext) {
101+
$response = new RedirectResponse('/setup/');
102+
$response->send();
103+
exit;
104+
}
98105
} else {
99106
if (file_exists(PMF_CONFIG_DIR . '/database.php')) {
100107
$databaseFile = PMF_CONFIG_DIR . '/database.php';

phpmyfaq/src/phpMyFAQ/Application.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,42 @@ private function handleRequest(
165165
)
166166
: 'Bad Request';
167167
$response = new Response(content: $message, status: Response::HTTP_BAD_REQUEST);
168+
} catch (Throwable $exception) {
169+
// Log the error for debugging
170+
error_log(sprintf(
171+
'Unhandled exception in Application: %s at %s:%d',
172+
$exception->getMessage(),
173+
$exception->getFile(),
174+
$exception->getLine(),
175+
));
176+
177+
$message = Environment::isDebugMode()
178+
? $this->formatExceptionMessage(
179+
template: 'Internal Server Error: :message at line :line at :file',
180+
exception: $exception,
181+
)
182+
: 'Internal Server Error';
183+
184+
// Return JSON response for API requests
185+
if (str_contains(haystack: $urlMatcher->getContext()->getBaseUrl(), needle: '/api')) {
186+
$content = Environment::isDebugMode()
187+
? json_encode(value: [
188+
'error' => 'Internal Server Error',
189+
'message' => $exception->getMessage(),
190+
'file' => $exception->getFile(),
191+
'line' => $exception->getLine(),
192+
])
193+
: json_encode(value: ['error' => 'Internal Server Error']);
194+
195+
$response = new Response(content: $content, status: Response::HTTP_INTERNAL_SERVER_ERROR, headers: [
196+
'Content-Type' => 'application/json',
197+
]);
198+
199+
$response->send();
200+
return;
201+
}
202+
203+
$response = new Response(content: $message, status: Response::HTTP_INTERNAL_SERVER_ERROR);
168204
}
169205

170206
$response->send();

0 commit comments

Comments
 (0)