Yii2 SEO: Pretty URLs, meta titles and meta descriptions

Author: Rafal Marguzewicz
Categories: Yii2

yii2 seo For search engine optimization SEO consists of many parts. I chose a few:

  1. Correct meta title, length of 60 characters
  2. Correct meta description, length of 140 characters, two sentences
  3. Heading H1 i H2
  4. Pretty URLs
  5. Set alternative text of images (attribute ALT)
  6. Using canonical URLs
  7. Using https and minimalism one [sub]domain

And like framework Yii2 help realize these points:

1. Correct meta title

In view

$this->title = "Meta title of web page"

In model or extension

Yii::$app->view->title = "Meta title of web page with 2-3 keyword" 

2. Correct meta description

			'name' => 'description',
			'content' => 'Very cool meta description. Help decide for user of search enginge.'

3. Heading H1 i H2

use yii\helpers\Html;

echo Html::tag('h1', 'Yii2 SEO: Pretty URLs, meta titles and meta descriptions' );
echo Html::tag('h2', 'Heading H2', ['class' => 'small'] );

4. Pretty URLs (search engine friendly)

To use prettu URLs have to add fallowing code to config file

'components' => [
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<controller:\w+>' => '<controller>/index',
		      '<controller:\w+>/<id:\d+>' => '/view',

In then we can create address URL with help fallowing code:

    <?= yii\helpers\Url::Url::to(['post/view', 'id' => 1]);?>  //  /post/view/1

5. Alternative text of images

<?= yii\helpers\Html::img('@web/images/photo.jpg', ['alt' => 'if you not see it, you can hear']);?>

6. Canonical URLs

  use Yii;
  Yii::$app->view->registerLinkTag(['rel' => 'canonical', 'href' => URL::to()]);

Bonus. Class SEO

Class SEO to better otimization of web page. e.g in model:

namespace app\models;

use Yii;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\BaseInflector;
use yii\helpers\StringHelper;

class Seo extends \yii\base\Widget {

public $model;
private $action;

	public function init(){
		if ($this->model === null) {
			print_r('model is required');die();
		$seo_title = BaseInflector::slug(StringHelper::truncateWords($this->model->title, 8), "-");
		$this->action = Url::to(['', 'id' => 1, 'seotitle' => $seo_title], true);

	public function title(){
		Yii::$app->view->title = ($this->model->meta_title != null) ? $this->model->meta_title : $this->model->title;

	public function description(){
			'name' => 'description',
			'content' => 'Meta descrition of the page for search engines e.g. DuckDuckGo'
	public function canonical(){
		Yii::$app->view->registerLinkTag(['rel' => 'canonical', 'href' => $this->canonical]);


<?php use common\models\Seo;

$seo = new Seo([
	'model' => $model,

	<h1><?= $model->title ?></h1>
	<?= $model->content?>

Popular search terms:

  1. Gabriel Alejandro López López says:

    For more SEO useful tags you can try https://github.com/daxslab/yii2-taggedview. Best regards from Cuba!

Your email address will not be published. Required fields are marked *

Name *
Email *

Read previous post:
Which Yii2 project template to choose, basic or advanced?

Before installation framework Yii2 we have to choose two version: Yii2 Basic Project Template and Yi2 Advanced Project Template Looking...