Skip to content

Commit 1a88e30

Browse files
committed
refactor: migrated solution id route (#3834)
1 parent b8425cd commit 1a88e30

File tree

6 files changed

+39
-22
lines changed

6 files changed

+39
-22
lines changed

.docker/frankenphp/Caddyfile

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
php_server
2929
}
3030

31-
# Solution ID pages
32-
@solution_id path_regexp solution ^/solution_id_([0-9]+)\.html$
33-
rewrite @solution_id /index.php?solution_id={re.solution.1}
34-
3531
# Google sitemap
3632
rewrite /sitemap.xml /index.php
3733
rewrite /sitemap.gz /index.php
@@ -106,10 +102,6 @@
106102
php_server
107103
}
108104

109-
# Solution ID pages
110-
@solution_id path_regexp solution ^/solution_id_([0-9]+)\.html$
111-
rewrite @solution_id /index.php?solution_id={re.solution.1}
112-
113105
# Google sitemap
114106
rewrite /sitemap.xml /index.php
115107
rewrite /sitemap.gz /index.php

.docker/nginx/default.conf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,6 @@ server {
217217

218218
location @rewriteapp {
219219

220-
# a solution id page
221-
rewrite ^/solution_id_([0-9]+)\.html$ /index.php?solution_id=$1 last;
222-
223220
# PMF Google sitemap
224221
rewrite ^/sitemap\.xml$ /index.php last;
225222
rewrite ^/sitemap\.gz$ /index.php last;

nginx.conf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ server {
8888

8989
location @rewriteapp {
9090

91-
# a solution id page
92-
rewrite ^/solution_id_([0-9]+)\.html$ /index.php?solution_id=$1 last;
93-
9491
# PMF Google sitemap
9592
rewrite ^/sitemap\.xml$ /index.php last;
9693
rewrite ^/sitemap\.gz$ /index.php last;

phpmyfaq/.htaccess

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ Header set Access-Control-Allow-Headers "Content-Type, Authorization"
104104
RewriteRule ^(admin/assets)($|/) - [L]
105105
# Error pages
106106
ErrorDocument 404 /404.html
107-
# a solution ID page
108-
RewriteCond %{REQUEST_URI} solution_id_([0-9]+)\.html$ [NC]
109-
RewriteRule ^solution_id_(.*)\.html$ index.php?solution_id=$1 [L,QSA]
110-
# phpMyFAQ news page
111-
RewriteRule ^news/([0-9]+)/([a-z\-_]+)/(.+)\.htm(l?)$ index.php?action=news&newsid=$1&newslang=$2 [L,QSA]
112107
# phpMyFAQ Google sitemap
113108
RewriteRule ^sitemap.xml$ index.php [L,QSA]
114109
RewriteRule ^sitemap.gz$ index.php [L,QSA]
@@ -136,7 +131,6 @@ Header set Access-Control-Allow-Headers "Content-Type, Authorization"
136131
RewriteCond %{REQUEST_URI} !index\.php$
137132
RewriteRule ^(.*/)?setup/(.*) $1setup/index.php [L,QSA]
138133
RewriteRule ^update$ update/ [R=301,L]
139-
140134
# Front controller: route all requests to index.php (Symfony Router)
141135
# Skip if the file or directory exists
142136
RewriteCond %{REQUEST_FILENAME} !-f

phpmyfaq/src/phpMyFAQ/Controller/Frontend/FaqController.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,42 @@ public function add(Request $request): Response
137137
return $this->render('add.twig', $templateVars);
138138
}
139139

140+
/**
141+
* Redirects solution_id URLs to the actual FAQ page
142+
*
143+
* @throws Exception|\Exception
144+
*/
145+
#[Route(path: '/solution_id_{solutionId}.html', name: 'public.faq.solution', methods: ['GET'])]
146+
public function solution(Request $request): Response
147+
{
148+
$solutionId = Filter::filterVar($request->attributes->get('solutionId'), FILTER_VALIDATE_INT, 0);
149+
150+
if ($solutionId === 0) {
151+
return new Response('', Response::HTTP_NOT_FOUND);
152+
}
153+
154+
$faq = $this->container->get('phpmyfaq.faq');
155+
$faqData = $faq->getIdFromSolutionId($solutionId);
156+
157+
if (empty($faqData)) {
158+
return new Response('', Response::HTTP_NOT_FOUND);
159+
}
160+
161+
// Generate a simple slug from the question title
162+
$slug = preg_replace('/[^a-z0-9]+/i', '-', strtolower($faqData['question'] ?? 'faq'));
163+
$slug = trim($slug, '-') ?: 'faq';
164+
165+
// Redirect to the canonical FAQ URL
166+
$url = sprintf('/faq/%d/%d/%s.html', $faqData['category_id'], $faqData['id'], $slug);
167+
168+
return new RedirectResponse($url, Response::HTTP_MOVED_PERMANENTLY);
169+
}
170+
140171
/**
141172
* Displays a single FAQ article with comments, ratings, and related content
142173
*
143-
* @throws Exception
174+
* @throws Exception|LoaderError*@throws \Exception
175+
*
144176
*/
145177
#[Route(path: '/faq/{categoryId}/{faqId}/{slug}.html', name: 'public.faq.show', methods: ['GET'])]
146178
public function show(Request $request): Response
@@ -265,7 +297,7 @@ public function show(Request $request): Response
265297
'title' => sprintf('%s - %s', $seoData->getTitle() ?? $question, $this->configuration->getTitle()),
266298
'metaDescription' => $seoData->getDescription(),
267299
'solutionId' => $faq->faqRecord['solution_id'],
268-
'solutionIdLink' => Link::getSystemRelativeUri() . '?solution_id=' . $faq->faqRecord['solution_id'],
300+
'solutionIdLink' => './solution_id_' . $faq->faqRecord['solution_id'] . '.html',
269301
'breadcrumb' => $category->getPathWithStartpage($cat, '/', true),
270302
'question' => $question,
271303
'answer' => $answer,

phpmyfaq/src/public-routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
'controller' => [FaqController::class, 'add'],
7070
'methods' => 'GET',
7171
],
72+
'public.faq.solution' => [
73+
'path' => '/solution_id_{solutionId}.html',
74+
'controller' => [FaqController::class, 'solution'],
75+
'methods' => 'GET',
76+
],
7277
'public.faq.show' => [
7378
'path' => '/faq/{categoryId}/{faqId}/{slug}.html',
7479
'controller' => [FaqController::class, 'show'],

0 commit comments

Comments
 (0)