Google trends Public Holidays Coupon Code Code Compiler

How to Create Dynamic XML Sitemap in Codeigniter


Nov 6, 2023

How to Create Dynamic XML Sitemap in Codeigniter

Learn how to create a dynamic XML sitemap for your Codeigniter website to improve search engine visibility. Follow our SEO-friendly guide, complete with example code.

Creating a dynamic XML sitemap in Codeigniter is a crucial step in optimizing your website for search engines. Sitemaps help search engines like Google crawl and index your site more efficiently, which can lead to improved search engine rankings and better visibility for your web pages. In this SEO-friendly guide, we'll walk you through the process of generating a dynamic XML sitemap for your Codeigniter-based website, and we'll provide you with example code to get you started.

Step 1: Set Up Codeigniter Project

Before you can create a dynamic XML sitemap, you need to have a Codeigniter project up and running. If you haven't already, install Codeigniter, and set up your website structure.

Step 2: Create the Sitemap Controller

In Codeigniter, controllers are used to handle the logic for specific webpages. Create a new controller for your sitemap. You can name it something like Sitemap.php.


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

class Sitemap extends CI_Controller {
    public function index() {
        header("Content-Type: text/xml;charset=iso-8859-1");
        $this->load->database();
        $articleList = $this->db->get("article")->result();
        $data['articles'] = $articleList;
        $this->load->view('sitemap', $data);
    }
}

Step 3: Create Route

In this route file, we create one “sitemap.xml” route for access from the URL. so open application/config/routes.php file.


<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
 
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
 
 
$route['sitemap\.xml'] = "Sitemap/index";

Step-4: Create a view file in your project folder.

In this step, We have to create a sitemap.php view file in path application/views/sitemap.php.  Now write the code of the XML file with a dynamic code.


<?php echo'<?xml version="1.0" encoding="UTF-8" ?>' ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
        <loc><?php echo base_url();?></loc>
		<lastmod><?php echo date('c', strtotime(date('Y-m-d H:i:s'))); ?></lastmod>
        <priority>1.0</priority>
        <changefreq>daily</changefreq>
    </url>
 
 
    <!-- Dynamic Sitemap -->

    <?php 
	 $lastmod = date('c', strtotime(date('Y-m-d H:i:s')));
	foreach($articles as $item) { ?>
    <url>
        <loc><?php echo base_url()."article/".$item->id ?></loc>
		<lastmod><?php echo $lastmod; ?></lastmod>
        <priority>0.85</priority>
        <changefreq>daily</changefreq>
    </url>
    <?php } ?>
</urlset>

You can run on the following URL to see sitemap:


http://yourwebsiteurl.com/sitemap.xml

Step 5: Submit Your Sitemap to Search Engines

After creating the dynamic XML sitemap, you should submit it to major search engines like Google and Bing through their respective webmaster tools. This will ensure that search engines regularly crawl and index your site, improving its visibility in search results.

By following these steps and using the provided example code, you can create a dynamic XML sitemap for your Codeigniter website, making it more SEO-friendly and increasing its chances of ranking well in search engine results pages.

Copyright 2024. All rights are reserved