How to Create a Dynamic Sitemap in CodeIgniter

Create Dynamic XML Sitemap in CodeIgniter

Learn how to generate a dynamic sitemap.xml using CodeIgniter 3 step by step.

CodeIgniter 3 Dynamic Sitemap SEO Tutorial

What is Sitemap?

A XML Sitemap is a file that contains the URLs of your website. Search engines like Google, Bing and Yahoo read this file to discover and crawl your pages more efficiently.

A sitemap can also include additional information such as:

  • Page URL
  • Last Modified Date
  • Priority
  • Change Frequency
Goal: In this tutorial we'll create a dynamic sitemap.xml using CodeIgniter and MySQL.

Step 1 — Create Database

Create a database named codeigniter_sitemap.

CREATE DATABASE codeigniter_sitemap;

Step 2 — Create Pages Table

Create a table called pages.

CREATE TABLE pages (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name TEXT NULL,
    url TEXT NULL,
    description LONGTEXT NULL
);
ColumnType
id INT
name TEXT
url TEXT
description LONGTEXT

Step 3 — Configure CodeIgniter

Open the following files:

  • application/config/config.php
  • application/config/autoload.php
$config['base_url'] = 'http://localhost/codeigniter-dynamic-sitemap/';

$autoload['helper'] = array('url');
$autoload['model'] = array('crud');
$autoload['libraries'] = array('database');

Step 4 — Database Connection

Edit application/config/database.php

$db['default'] = array(
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'codeigniter_sitemap',
    'dbdriver' => 'mysqli',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci'
);

Step 5 — Create Route

Open application/config/routes.php

$route['sitemap\.xml'] = "welcome/sitemap";
Visiting /sitemap.xml will now call the sitemap() function.

Step 6 — Create Crud Model

Now create a model file named Crud.php inside the application/models/ folder.

The model will retrieve all records from the pages table using CodeIgniter's database library.

File location:

application/models/Crud.php

Complete Code:

<?php

class Crud extends CI_Model
{
public function get_data($table)
{
$data = $this->db->get($table);

    return $data->result();
}


}
How it works: The get_data() function receives the table name, fetches all records from that table and returns the results as an array of objects.

In our example, the controller will call:

$this->crud->get_data("pages");

This will retrieve all records from the pages table.


Step 7 — Create Sitemap Controller

Edit application/controllers/Welcome.php

public function sitemap()
{
    $data['ALLPAGES'] = $this->crud->get_data("pages");

    header("Content-Type: text/xml;charset=utf-8");

    $this->load->view("sitemap", $data);
}
The controller loads all pages from the database and returns an XML response.

Step 8 — Create sitemap.php View

Now create a view file named sitemap.php inside the application/views/ folder.

File location:

application/views/sitemap.php

This view will generate the XML Sitemap using the page URLs retrieved from the database.

Complete Code:

<?xml version="1.0" encoding="UTF-8"?>



<?php foreach ($ALLPAGES as $page) { ?>

    

        <?= base_url($page->url); ?>

        0.5

    

<?php } ?>

How it works: The foreach loop goes through every record in the pages table and creates a separate entry for each page.

Example XML Output

Suppose your database contains these URLs:

/about-us

/courses
/contact-us

The generated sitemap will look similar to:

<?xml version="1.0" encoding="UTF-8"?>




    https://www.example.com/about-us
    0.5



    https://www.example.com/courses
    0.5



    https://www.example.com/contact-us
    0.5

Important: Do not paste the example XML output into your sitemap.php file. The first code block is the actual PHP view code. The second example only shows what the browser/search engine will receive after PHP generates the sitemap.

Final Output

Open the following URL in your browser:

http://localhost/codeigniter-dynamic-sitemap/sitemap.xml
If everything is configured correctly, you'll see a dynamically generated XML Sitemap containing all page URLs from your database.

Project Folder Structure

application
│
├── config
│   ├── config.php
│   ├── routes.php
│   └── database.php
│
├── controllers
│   └── Welcome.php
│
├── models
│   └── Crud.php
│
└── views
    └── sitemap.php

Conclusion

Congratulations! You have successfully created a dynamic XML Sitemap in CodeIgniter. Whenever a new page is added to the pages table, it will automatically appear in sitemap.xml without manually editing the file.

This is the recommended approach for SEO because search engines can always discover your latest pages through an updated sitemap.