Commit e21450c9f471844a427d247816540e8f15a644eb

Authored by Dmytry Fedorchuk
1 parent 173e7c8a

SEO

.htaccess
... ... @@ -15,6 +15,7 @@ AddDefaultCharset utf-8
15 15  
16 16  
17 17  
  18 +
18 19 RewriteRule ^storage/(.*)?$ /storage/$1 [L,PT]
19 20  
20 21 RewriteCond %{REQUEST_URI} ^/(admin)
... ... @@ -70,14 +71,16 @@ AddDefaultCharset utf-8
70 71  
71 72 RewriteCond %{REQUEST_FILENAME} !-d
72 73  
73   - RewriteCond %{HTTP_HOST} ^www\.(.*)
74   - RewriteRule ^(.*)$ frontend/web/index.php [L]
75 74  
76   - RewriteCond %{HTTP_HOST} ^([^www].*)$
77   - RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  75 + RewriteCond %{HTTP_HOST} ^www\.(.*)
  76 + RewriteRule ^(.*)$ frontend/web/index.php
78 77  
79 78  
  79 + RewriteCond %{HTTP_HOST} ^([^www].*)$
  80 + RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
80 81  
  82 + RewriteCond %{THE_REQUEST} (\/[\w\/-]+\w)\s
  83 + RewriteRule (.*) %1/ [R,L]
81 84  
82 85  
83 86  
... ...
frontend/views/site/index.php
... ... @@ -5,10 +5,10 @@ use yii\web\View;
5 5 use frontend\widgets\BannerWidget;
6 6 use yii\helpers\Url;
7 7 use frontend\widgets\Seo;
8   -$this->params['seo']['seo_text'] = 'TEST SEO TEXT';
9   -$this->params['seo']['h1'] = 'TEST H1';
10   -$this->params['seo']['description'] = 'TEST DESCRIPTION';
11   -$this->params['seo']['fields']['name'] = 'TEST NAME FROM FIELD';
  8 +//$this->params['seo']['seo_text'] = 'TEST SEO TEXT';
  9 +//$this->params['seo']['h1'] = 'TEST H1';
  10 +//$this->params['seo']['description'] = 'TEST DESCRIPTION';
  11 +//$this->params['seo']['fields']['name'] = 'TEST NAME FROM FIELD';
12 12 $this->params['seo']['title'] = 'Купить городской рюкзак Киев | Купить рюкзаки Dakine Украина | Rukzachok.com.ua';
13 13 $this->registerJsFile (
14 14 Yii::$app->request->baseUrl . '/js/slides.min.jquery.js',
... ...
frontend/widgets/Seo.php
1 1 <?php
2 2 namespace frontend\widgets;
  3 +
3 4 use common\models\SeoDynamic;
4 5 use yii\base\Widget;
5 6 use yii\helpers\Html;
... ... @@ -7,9 +8,9 @@ use yii\helpers\Html;
7 8 class Seo extends Widget
8 9 {
9 10 private $url;
10   - public $row;
11   - public $own_attr;
12   - public $fields;
  11 + public $row;
  12 + public $own_attr;
  13 + public $fields;
13 14 public $description;
14 15 public $title;
15 16 public $meta;
... ... @@ -19,17 +20,16 @@ class Seo extends Widget
19 20 public $project_name;
20 21  
21 22  
22   -
23   -
24 23 const SEO_TEXT = 'seo_text';
25 24 const DESCRIPTION = 'description';
26 25 const META = 'meta';
27 26 const H1 = 'h1';
28 27 const TITLE = 'title';
29 28  
30   - public function init(){
31   - $this->url = \Yii::$app->request->url;
32   - $this->project_name = \Yii::$app->name;
  29 + public function init()
  30 + {
  31 + $this->url = \Yii::$app->request->url;
  32 + $this->project_name = \Yii::$app->name;
33 33 parent::init();
34 34  
35 35 }
... ... @@ -39,7 +39,7 @@ class Seo extends Widget
39 39 {
40 40  
41 41 $seoData = $this->getViewData();
42   - foreach($seoData as $key=>$value){
  42 + foreach ($seoData as $key => $value) {
43 43 $this->$key = $value;
44 44 }
45 45  
... ... @@ -99,37 +99,43 @@ class Seo extends Widget
99 99 $meta = $this->selectSeoData(self::META);
100 100  
101 101 $filter = \Yii::$app->request->get('filter', []);
  102 + $sort = \Yii::$app->request->get('sort', []);
102 103  
103   -
104   -
105   - if(!empty($meta)){
  104 + if (!empty($meta)) {
106 105  
107 106 $this->getView()->registerMetaTag([
108 107 'name' => 'robots',
109   - 'content' => $meta
  108 + 'content' => $meta
110 109 ]);
111 110  
112   - } else if (
  111 + } else if (
113 112 isset($filter['brands']) && count($filter['brands']) > 1
114 113 || isset($filter['options']["pol"]) && count($filter['options']["pol"]) > 1
115 114 || isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) > 1
116 115 || isset($filter['options']["god"]) && count($filter['options']["god"]) > 1
117   - || isset($filter['brands']) && count($filter['brands']) <= 1 && isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) >= 4
118   - || isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) > 4
119 116  
120   - ){
121   - $this->getView()->registerMetaTag([
122   - 'name' => 'robots',
123   - 'content' => 'noindex,follow'
124   - ]);
  117 + ) {
  118 + $this->getView()->registerMetaTag([
  119 + 'name' => 'robots',
  120 + 'content' => 'noindex,nofollow'
  121 + ]);
125 122  
126   - } else if($filter) {
  123 + } else if (
  124 + isset($filter['brands']) && count($filter['brands']) <= 1 && isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) >= 4
  125 + || isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) > 4
  126 + || !empty($sort) || isset($filter['prices'])
  127 + ) {
  128 + $this->getView()->registerMetaTag([
  129 + 'name' => 'robots',
  130 + 'content' => 'noindex,follow'
  131 + ]);
  132 + } else {
127 133  
128   - $this->getView()->registerMetaTag([
129   - 'name' => 'robots',
130   - 'content' => 'index,follow'
131   - ]);
132   - }
  134 + $this->getView()->registerMetaTag([
  135 + 'name' => 'robots',
  136 + 'content' => 'index,follow'
  137 + ]);
  138 + }
133 139  
134 140  
135 141 break;
... ... @@ -141,9 +147,9 @@ class Seo extends Widget
141 147 protected function replaceData($str)
142 148 {
143 149  
144   - if(!empty($this->fields)){
145   - foreach($this->fields as $field_name => $field_value){
146   - $str = str_replace('{'.$field_name.'}', $field_value, $str);
  150 + if (!empty($this->fields)) {
  151 + foreach ($this->fields as $field_name => $field_value) {
  152 + $str = str_replace('{' . $field_name . '}', $field_value, $str);
147 153 }
148 154 }
149 155 $str = str_replace('{project_name}', $this->project_name, $str);
... ... @@ -152,47 +158,49 @@ class Seo extends Widget
152 158  
153 159 protected function findSeoByUrl()
154 160 {
155   - return \common\models\Seo::findOne(['url'=>$this->url]);
  161 + return \common\models\Seo::findOne(['url' => $this->url]);
156 162 }
157 163  
158 164 protected function findSeoByDynamic()
159 165 {
160   - $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller'=> \Yii::$app->controller->id, 'action'=>\Yii::$app->controller->action->id]);
161   - if(!empty($this->key)){
162   - $query->andWhere(['key'=>$this->key]);
  166 + $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller' => \Yii::$app->controller->id, 'action' => \Yii::$app->controller->action->id]);
  167 + if (!empty($this->key)) {
  168 + $query->andWhere(['key' => $this->key]);
163 169 }
164 170 return $query->one();
165 171 }
166 172  
167 173  
168   - protected function getViewData(){
  174 + protected function getViewData()
  175 + {
169 176 $params = $this->getView()->params;
170   - if(isset($params['seo'])){
  177 + if (isset($params['seo'])) {
171 178 return $params['seo'];
172 179 } else {
173 180 return [];
174 181 }
175 182 }
176 183  
177   - protected function selectSeoData($param){
  184 + protected function selectSeoData($param)
  185 + {
178 186 $result = '';
179 187  
180 188 $widgetData = $this->findSeoByUrl();
181 189  
182   - if($widgetData instanceof \common\models\Seo){
  190 + if ($widgetData instanceof \common\models\Seo) {
183 191  
184   - $result = $widgetData->$param;
  192 + $result = $widgetData->$param;
185 193  
186   - }else if(!empty($this->$param)){
  194 + } else if (!empty($this->$param)) {
187 195  
188 196 $result = $this->$param;
189 197 } else {
190 198  
191 199 $widgetData = $this->findSeoByDynamic();
192 200  
193   - if($widgetData instanceof SeoDynamic){
  201 + if ($widgetData instanceof SeoDynamic) {
194 202  
195   - $result = $widgetData->$param;
  203 + $result = $widgetData->$param;
196 204  
197 205 }
198 206  
... ...