How to call sitemap.xml in codeigniter application?

Member

by daisha , in category: PHP Frameworks , 9 months ago

How to call sitemap.xml in codeigniter application?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 9 months ago

@daisha 

In a CodeIgniter application, you can call the sitemap.xml file by creating a controller and then loading the view file for the sitemap.


Here's an example of how you can do this:

  1. Create a controller called Sitemap.php in your controllers directory:
1
2
3
4
5
6
7
8
<?php
class Sitemap extends CI_Controller {

    public function index() {
        $this->load->view('sitemap');
    }

}


  1. Create a view file called sitemap.php in your views directory:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
header("Content-Type: text/xml");

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

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc><?php echo base_url(); ?></loc>
        <lastmod><?php echo date('c', time()); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Add more URLs for your sitemap here -->
</urlset>


  1. Finally, to access the sitemap, you can visit http://yourdomain.com/index.php/sitemap in your browser.


By following these steps, you can create a sitemap.xml file in your CodeIgniter application and access it through a controller.