Skip to content Skip to sidebar Skip to footer

How to Create a Sitemap for a Blog with CodeIgniter 3.1.11

What is a Sitemap? And what's its use for our blog?

Based on references I've read, such as Wikipedia, a Sitemap is a map of a site that can help Webmasters. A webmaster is a person responsible for managing and maintaining one or more websites. They are usually associated with web developers, site authors, website administrators, website owners, website coordinators, or website publishers. A Sitemap can help these webmasters introduce the content available on their website. The Sitemap needs to be registered on the Google Search Console in a .xml file format so that Google knows the content available in the Sitemap and matches it with the URL in the Google search index.

This tutorial is an update from the Ilmu Coding website because there are some formats, like the tag urlset and date, that don't match in Google Search Console. In this tutorial, I'm using CodeIgniter version 3.1.11, so it's recommended to use version 3.1.x and above, not version 4.0.4, because the methods will differ. If you don't have one, please download it from the official site.

Step 1: Create a New Database

Create a database with the name db_blog. You can adjust the database name or if you want to follow this tutorial, just stick to the name db_blog. Because this tutorial uses the localhost server, for the web server you can use XAMPP for Windows or WAMPP for Linux.

Step 2: Create a Table Named artikel

After successfully creating the database, let's continue to make an artikel table within the previously created db_blog database. To create the artikel table, you can type or copy-paste the following SQL query:

CREATE TABLE `db_blog`.`artikel` (
`a_tanggal` DATE NOT NULL , 
`a_judul` VARCHAR(100) NOT NULL , 
`a_slug` VARCHAR(100) NOT NULL , 
PRIMARY KEY (`a_id`)) ENGINE = InnoDB;

Then, add some data to the table. For a quicker process, simply copy-paste the code below:

INSERT INTO `artikel` (`a_id`, `a_tanggal`, `a_judul`, `a_slug`) VALUES 
(NULL, '2020-09-21', 'My First Post', 'my-first-post'), 
(NULL, '2020-09-22', 'My Second Post', 'my-second-post'), 
(NULL, '2020-09-23', 'My Third Post', 'my-third-post')

Why NULL? Because we set the data type a_id to AUTO_INCREMENT, so the stored data will automatically increment by 1 in the artikel table.

Step 3: Configure CodeIgniter 3.1.11

If you haven't downloaded the file as mentioned earlier, please download it from the official site. Then, move the downloaded file and extract the folder to the htdocs directory if you're using xampp or www if you're using wampp. Rename the folder to ci_sitemap.

Step 4: Configure the Database in CodeIgniter

Open the database.php file in the CodeIgniter directory, which is application/config. Then, change the values for hostname, username, password, and database like this:

'hostname' => 'localhost',

'username' => 'root',

'password' => '',

'database' => 'db_blog',

Still, in the application/config directory, we will create a database library in CodeIgniter so that it can run automatically. To do this, open the autoload.php file, then change the code $autoload['libraries'] = array(); to $autoload['libraries'] = array('database');

And also the autoload for our helper is changed like this $autoload['helper'] = array('url'); so that functions like base_url or site_url can be run automatically without having to declare their load function. Example:


Step 5: Configure URL and .htaccess

Since CodeIgniter doesn't provide a URL for the .xml format, we have to create it manually. 

Let's try.

Open the file routes.php in the directory application/config. Then add the following code here:

$route['sitemap\.xml'] = 'sitemap';

So, it will become.

$route['sitemap\.xml'] = 'sitemap';
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

The code above is made for the controller with the file name Sitemap.php so that it can access the URL with the format sitemap.xml.

For instance:


You might wonder, what's the use of htaccess? Now, if you look at the URL above, it seems the URL is not a friendly-looking thing because it includes index.php. Therefore, we will remove index.php so that the URL will look like this:


To do this, create a new file .htaccess in the directory ci_sitemap, so it's not inside the directory application but outside that directory. Then add the following code:

Options -Indexes
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Next, we set the base_url dynamically in CodeIgniter.

Let's try it.

Open file config.php in the directory application/config, then update this code $config['base_url'] = ''; into this:

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://" . $_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);

Don't forget to remove index.php in file config.php, so that it will become $config['index_page'] = '';

Step 6: Create a Sitemap Model in CodeIgniter

Create a new file Sitemap_model.php in the application/models directory. Then, type the following code:

defined('BASEPATH') OR exit('No direct script access allowed');

class Sitemap_model extends CI_Model
 private static $_table = 'artikel';

 public function get_artikel()
  $query = $this->db->select('*')->from(self::$_table)->order_by('a_tanggal', 'DESC')->get();

  $results = array();
        if ($query->num_rows() > 0) {
         $results = $query->result_array();
        return $results;

Step 7: Create a Sitemap Controller in CodeIgniter

Next, create a new file Sitemap.php in the application/controllers directory. Then, add the code below:

defined('BASEPATH') OR exit('No direct script access allowed');

class Sitemap extends CI_Controller

 public function __construct()

 public function index()
  $data['post'] = $this->sitemap_model->get_artikel();
        $this->load->view('sitemap/index', $data);

In the code above, if you look at the code $data['post'] = $this->sitemap_model->get_artikel($where); We call the function get_artikel() that is in Sitemap_model.php then we parse that data and send it to the view.

Step 8: Create a Sitemap View in CodeIgniter

The final step is to create a sitemap view in the .xml format. Create a new folder sitemap in the application/views directory, then create a new file and save it as index.php. Next, you can type the following code:

Now, it's my turn to explain what that code means.

The third line of our code is crucial. It's the header that ensures XML tags are properly displayed. It's important to include XML tags exactly as shown in the example code. Incorrect or missing tags can lead Google to misinterpret your XML structure, potentially causing errors to appear in your Google Search Console after you've registered your sitemap.

Tag url is used to display the URL that will be registered on the Google Search Console sitemap.

The loc tag specifically is for entering the URL to be registered on the sitemap, which is typically the URL of your blog posts.

For the lastmod tag, you can specify when the URL was last modified. It's essential to adhere to the format shown in the code. Any deviation from this format might result in Google failing to correctly recognize the lastmod data based on my personal experience.

The tag changefreq can be filled with values like daily, weekly, or monthly. However, I recommend using daily to prompt Google to check this URL every day.

Finally, the priority tag influences how quickly Google considers scanning your URL. You can assign values like 0.1 or 0.5, but for this tutorial, we'll use 0.1.

The rest of the code is a loop that adds sitemap entries based on the number of articles in our database.

Testing the Sitemap Implementation

We will now try to run the codes we've created earlier.

Okay, let's go. Let's try it.

Visit this URL:


If successful without any errors, then the page will appear like this:


We've successfully created a sitemap in CodeIgniter and are ready to register it on Google Search Console. If you have questions or face any issues, please leave a comment in the comment section below.

To support this sitemap tutorial, I've provided a link to download the source code of this application via GitHub. You can download it here:

Download Source Code

Rinaldi Pratama Putra
Rinaldi Pratama Putra Reality is a lovely place, but I wouldn't wanna live there.