From e21450c9f471844a427d247816540e8f15a644eb Mon Sep 17 00:00:00 2001 From: mr Date: Mon, 13 Jun 2016 10:20:26 +0300 Subject: [PATCH] SEO --- .htaccess | 11 +++++++---- frontend/views/site/index.php | 8 ++++---- frontend/widgets/Seo.php | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ 3 files changed, 61 insertions(+), 50 deletions(-) diff --git a/.htaccess b/.htaccess index 78c62ba..ee89add 100644 --- a/.htaccess +++ b/.htaccess @@ -15,6 +15,7 @@ AddDefaultCharset utf-8 + RewriteRule ^storage/(.*)?$ /storage/$1 [L,PT] RewriteCond %{REQUEST_URI} ^/(admin) @@ -70,14 +71,16 @@ AddDefaultCharset utf-8 RewriteCond %{REQUEST_FILENAME} !-d - RewriteCond %{HTTP_HOST} ^www\.(.*) - RewriteRule ^(.*)$ frontend/web/index.php [L] - RewriteCond %{HTTP_HOST} ^([^www].*)$ - RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] + RewriteCond %{HTTP_HOST} ^www\.(.*) + RewriteRule ^(.*)$ frontend/web/index.php + RewriteCond %{HTTP_HOST} ^([^www].*)$ + RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301] + RewriteCond %{THE_REQUEST} (\/[\w\/-]+\w)\s + RewriteRule (.*) %1/ [R,L] diff --git a/frontend/views/site/index.php b/frontend/views/site/index.php index a1429c6..7e8470c 100755 --- a/frontend/views/site/index.php +++ b/frontend/views/site/index.php @@ -5,10 +5,10 @@ use yii\web\View; use frontend\widgets\BannerWidget; use yii\helpers\Url; use frontend\widgets\Seo; -$this->params['seo']['seo_text'] = 'TEST SEO TEXT'; -$this->params['seo']['h1'] = 'TEST H1'; -$this->params['seo']['description'] = 'TEST DESCRIPTION'; -$this->params['seo']['fields']['name'] = 'TEST NAME FROM FIELD'; +//$this->params['seo']['seo_text'] = 'TEST SEO TEXT'; +//$this->params['seo']['h1'] = 'TEST H1'; +//$this->params['seo']['description'] = 'TEST DESCRIPTION'; +//$this->params['seo']['fields']['name'] = 'TEST NAME FROM FIELD'; $this->params['seo']['title'] = 'Купить городской рюкзак Киев | Купить рюкзаки Dakine Украина | Rukzachok.com.ua'; $this->registerJsFile ( Yii::$app->request->baseUrl . '/js/slides.min.jquery.js', diff --git a/frontend/widgets/Seo.php b/frontend/widgets/Seo.php index ca50ade..ddc3a31 100755 --- a/frontend/widgets/Seo.php +++ b/frontend/widgets/Seo.php @@ -1,5 +1,6 @@ url = \Yii::$app->request->url; - $this->project_name = \Yii::$app->name; + public function init() + { + $this->url = \Yii::$app->request->url; + $this->project_name = \Yii::$app->name; parent::init(); } @@ -39,7 +39,7 @@ class Seo extends Widget { $seoData = $this->getViewData(); - foreach($seoData as $key=>$value){ + foreach ($seoData as $key => $value) { $this->$key = $value; } @@ -99,37 +99,43 @@ class Seo extends Widget $meta = $this->selectSeoData(self::META); $filter = \Yii::$app->request->get('filter', []); + $sort = \Yii::$app->request->get('sort', []); - - - if(!empty($meta)){ + if (!empty($meta)) { $this->getView()->registerMetaTag([ 'name' => 'robots', - 'content' => $meta + 'content' => $meta ]); - } else if ( + } else if ( isset($filter['brands']) && count($filter['brands']) > 1 || isset($filter['options']["pol"]) && count($filter['options']["pol"]) > 1 || isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) > 1 || isset($filter['options']["god"]) && count($filter['options']["god"]) > 1 - || isset($filter['brands']) && count($filter['brands']) <= 1 && isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) >= 4 - || isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) > 4 - ){ - $this->getView()->registerMetaTag([ - 'name' => 'robots', - 'content' => 'noindex,follow' - ]); + ) { + $this->getView()->registerMetaTag([ + 'name' => 'robots', + 'content' => 'noindex,nofollow' + ]); - } else if($filter) { + } else if ( + isset($filter['brands']) && count($filter['brands']) <= 1 && isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) >= 4 + || isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) > 4 + || !empty($sort) || isset($filter['prices']) + ) { + $this->getView()->registerMetaTag([ + 'name' => 'robots', + 'content' => 'noindex,follow' + ]); + } else { - $this->getView()->registerMetaTag([ - 'name' => 'robots', - 'content' => 'index,follow' - ]); - } + $this->getView()->registerMetaTag([ + 'name' => 'robots', + 'content' => 'index,follow' + ]); + } break; @@ -141,9 +147,9 @@ class Seo extends Widget protected function replaceData($str) { - if(!empty($this->fields)){ - foreach($this->fields as $field_name => $field_value){ - $str = str_replace('{'.$field_name.'}', $field_value, $str); + if (!empty($this->fields)) { + foreach ($this->fields as $field_name => $field_value) { + $str = str_replace('{' . $field_name . '}', $field_value, $str); } } $str = str_replace('{project_name}', $this->project_name, $str); @@ -152,47 +158,49 @@ class Seo extends Widget protected function findSeoByUrl() { - return \common\models\Seo::findOne(['url'=>$this->url]); + return \common\models\Seo::findOne(['url' => $this->url]); } protected function findSeoByDynamic() { - $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller'=> \Yii::$app->controller->id, 'action'=>\Yii::$app->controller->action->id]); - if(!empty($this->key)){ - $query->andWhere(['key'=>$this->key]); + $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller' => \Yii::$app->controller->id, 'action' => \Yii::$app->controller->action->id]); + if (!empty($this->key)) { + $query->andWhere(['key' => $this->key]); } return $query->one(); } - protected function getViewData(){ + protected function getViewData() + { $params = $this->getView()->params; - if(isset($params['seo'])){ + if (isset($params['seo'])) { return $params['seo']; } else { return []; } } - protected function selectSeoData($param){ + protected function selectSeoData($param) + { $result = ''; $widgetData = $this->findSeoByUrl(); - if($widgetData instanceof \common\models\Seo){ + if ($widgetData instanceof \common\models\Seo) { - $result = $widgetData->$param; + $result = $widgetData->$param; - }else if(!empty($this->$param)){ + } else if (!empty($this->$param)) { $result = $this->$param; } else { $widgetData = $this->findSeoByDynamic(); - if($widgetData instanceof SeoDynamic){ + if ($widgetData instanceof SeoDynamic) { - $result = $widgetData->$param; + $result = $widgetData->$param; } -- libgit2 0.21.4