且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Codeigniter 生成站点地图

更新时间:2023-02-07 13:13:36

你可以使用我的代码:

控制器/seo.php

controllers/seo.php

Class Seo extends CI_Controller {

    function sitemap()
    {

        $data = "";//select urls from DB to Array
        header("Content-Type: text/xml;charset=iso-8859-1");
        $this->load->view("sitemap",$data);
    }
}

views/sitemap.php

views/sitemap.php

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

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc><?= base_url();?></loc> 
        <priority>1.0</priority>
    </url>

    <!-- My code is looking quite different, but the principle is similar -->
    <?php foreach($data as $url) { ?>
    <url>
        <loc><?= base_url().$url ?></loc>
        <priority>0.5</priority>
    </url>
    <?php } ?>

</urlset>

在 config/routes.php 中添加一行

add line to config/routes.php

$route['seo/sitemap.xml'] = "seo/sitemap";

对不起,如果代码中有一些错误,我特地为您制作的.如果有错误,您可以通过了解原理轻松修复它们.

Sorry if there are some errors in the code, I made it especially for you. If there are errors, you can fix them easily by understanding the principle.