Search engine optimization

Kiwa offers a lot of tools to help reaching a high level of search engine optimization. This page is about how to use them.

Table of contents

Meta Tags

The meta tags for each page can be defined in the main config file. For example:

<?php

$configGenerator
    ->setDefaultRobots('index, follow')
    ->setDefaultViewport('width=device-width, initial-scale=1')
    ->addPage(PageGenerator::create()
        ->setFile('index')
        ->setPageTitle('The page title')
        ->setOGTitle('The extended page title')
        ->setDescription('The description for this page.')
        ->setOGDescription('The extended description for this page.')
        ->setPageType('website')
        // More options are needed here but not important for this example.
    )
;

In this example, the index.phtml will be loaded. In this file, all the meta tags can be access and printed be writing

<?php

use Kiwa\Templating\MetaTags;

echo new MetaTags();

The output will be:

<meta property="generator" content="Kiwa">
<meta name="robots" content="index, follow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content="The extended page title">
<meta name="description" content="The description for this page.">
<meta property="og:description" content="The extended description for this page.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://www.example.org">

Manipulating the existing meta tags

All the information coming from the config file can be manipulated to a later point. This helps to handle dynamic pages as well.

To change the meta information, use the MetaTags class with the static method add:

<?php

use Kiwa\Templating\MetaTags;

MetaTags::add('og:title', 'The greatest website for the year ' . date('Y'));

When working with different languages, search engines prefer alternate links that help to understand which pages belong together. Kiwa handles these relations by its own.

The proper handle language version, you should route different pages to the same file:

<?php

$configGenerator
    // The english version
    ->addPage(PageGenerator::create()
        ->setName('imprint')
        ->setLanguageCode('en')
        ->setFile('imprint')
        // More options are needed here but not important for this example.
    )
    // The german version
    ->addPage(PageGenerator::create()
        ->setName('impressum')
        ->setLanguageCode('de')
        ->setFile('imprint')
        // More options are needed here but not important for this example.
    )
;

In this example we defined two pages with the routes /imprint.html and /impressum.html that are using the same file imprint.phtml. This helps Kiwa to understand that the two pages belong together.

All you need to do now is to print the link tags in your page header by calling

<?php

use Kiwa\Templating\AlternateLinks;

echo new AlternateLinks();

The output will be

<link rel="alternate" href="https://www.example.org/imprint.html" hreflang="en">
<link rel="alternate" href="https://www.example.org/impressum.html" hreflang="de">

The existing information about the related pages is stored in the RelatedPages class. They can be manipulated at any point and are needed to handle dynamic pages as well.

To manually set the alternate links, use the RelatedPages class with the static method addPage:

<?php

use Kiwa\Page\RelatedPages;

RelatedPages::addPage('fr', 'mentions-legales');

Canonical Tags

Kiwa provides a class to handle canonical tags. It will be used mostly to handle uppercase and lowercase URLs that request the same route.

The canonical tags can be accessed in the page header by calling

<?php

use Kiwa\Templating\Canonicals;

echo new Canonicals();

This will result in

<link rel="canonical" href="https://www.example.org/foo/bar.html">

To manually define the canonical tags, use the Canonicals class with the static method add:

<?php 

use Kiwa\Templating\Canonicals;

Canonicals::add('https://www.example.org/foo/bar.html');