diff --git a/common/models/SlashRedirect.php b/common/models/SlashRedirect.php new file mode 100644 index 0000000..78e548b --- /dev/null +++ b/common/models/SlashRedirect.php @@ -0,0 +1,36 @@ +link = trim($url, '/'); + return true; + } + return false; + } + + /** + * @return string + */ + public function getLink(): string + { + return $this->link; + } + } \ No newline at end of file diff --git a/frontend/components/UrlManager.php b/frontend/components/UrlManager.php index b55f706..d8ed517 100644 --- a/frontend/components/UrlManager.php +++ b/frontend/components/UrlManager.php @@ -3,6 +3,7 @@ namespace frontend\components; use artbox\core\models\Alias; + use artbox\core\models\interfaces\RedirectInterface; use artbox\core\services\Languages; use yii\helpers\Json; use yii\web\Request; @@ -42,6 +43,8 @@ * @param \artbox\core\services\Languages $languages * @param array $config */ + + public $redirects = []; public function __construct(Languages $languages, array $config = []) { $this->languages = $languages; @@ -62,7 +65,13 @@ // $this->checkRedirect($request->url); $request = $this->parseLanguage($request); - + foreach ($this->redirects as $redirectClass) { + /** + * @var \artbox\core\models\interfaces\RedirectInterface $redirect + */ + $redirect = \Yii::createObject($redirectClass); + $this->applyRedirect($redirect, $request->pathInfo); + } /** * @var Alias $alias */ @@ -210,4 +219,15 @@ // ->send(); // } } + + public function applyRedirect(RedirectInterface $redirect, string $url) + { + if ($redirect->doRedirect($url)) { + // var_dump($redirect->getLink());die(); + header("HTTP/1.1 301 Moved Permanently"); + header("Location: /" . $redirect->getLink()); + exit(); + } + return 0; + } } \ No newline at end of file diff --git a/frontend/config/main.php b/frontend/config/main.php index 1b1b86f..3e08749 100755 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -1,6 +1,6 @@ function () { - - $pathInfo = Yii::$app->request->pathInfo; - $query = Yii::$app->request->queryString; - if (!empty($pathInfo) && substr($pathInfo, -1) === '/' && substr($pathInfo, 0, 1) !== "/") { - $url = '/' . substr($pathInfo, 0, -1); - if ($query) { - $url .= '?' . $query; - } - Yii::$app->response->redirect($url, 301); - } # редирект с www.siteName.com//////////////////////// на www.siteName.com - elseif (substr($pathInfo, 0, 1) == "/" && substr($pathInfo, 1, 1) == "/") { - Yii::$app->response->redirect('/', 301); - } - }, 'id' => 'app-frontend', 'homeUrl' => '/', 'basePath' => dirname(__DIR__), @@ -382,6 +367,9 @@ use yii\helpers\Url; ], 'hideDefaultLanguagePrefix' => true, + 'redirects' => [ + SlashRedirect::className(), + ], ], 'assetsAutoCompress' => [ 'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent', -- libgit2 0.21.4