Answer the question
In order to leave comments, you need to log in
What is the most elegant way to solve the trailing slash problem in Yii2?
The problem is not new, but so far I have not found a clear solution.
Let's say there are 2 url options:
site.com/index
site.com/index/
'suffix' => '/',
Answer the question
In order to leave comments, you need to log in
https://github.com/yiisoft/yii2/issues/7670
$config = [
// ...
'params' => require(__DIR__ . '/params.php'),
// redirect to the page with the trailing slash
'on beforeRequest' => function () {
$app = Yii::$app;
$pathInfo = $app->request->pathInfo;
if (!empty($pathInfo) && substr($pathInfo, -1) !== '/') {
$app->response->redirect('/' . rtrim($pathInfo) . '/', 301);
}
},
];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question