使用Codeigniter生成站点地图

使用Codeigniter生成站点地图,codeigniter,frameworks,sitemap,Codeigniter,Frameworks,Sitemap,我需要在Codeigniter应用程序中生成一个站点地图。我发现了一些库,但它们都过时了,并且有bug 我真的需要一个单独的库吗? 我想知道在Codeigniter中生成站点地图的最佳方法。您可以使用我的代码: controllers/seo.php Class Seo extends CI_Controller { function sitemap() { $data = "";//select urls from DB to Array h

我需要在Codeigniter应用程序中生成一个站点地图。我发现了一些库,但它们都过时了,并且有bug

我真的需要一个单独的库吗?

我想知道在Codeigniter中生成站点地图的最佳方法。

您可以使用我的代码:

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

<?= '<?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>
对不起,如果代码中有一些错误,我是专门为你做的。 如果有错误,您可以通过理解原理轻松修复它们。

必须设置标题:

<?php header('Content-type: text/xml'); ?>
<?= '<?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>

1
0.5

强烈建议将站点地图的链接添加到robots.txt,如下所示:

Sitemap: http://www.yoursite.com/seo/sitemap

我已经编写了一个CodeIgniter模型,它使您能够从站点地图控制器调用函数,并在完成站点地图的输入后吐出XML

请随意查看并重用CodeIgniter型号:


如果你有很多页面/文章,那么多个网站地图怎么办?为什么这部分在echo中?非常感谢。它的工作,但它创建了一个错误时,提交给网站管理员帐户说“404错误”,其中作为链接似乎工作良好。这是链接检查,让我知道这对于指定了1000多条路径的站点是不正确的。你给了我一个线索。非常感谢,我的朋友!
Sitemap: http://www.yoursite.com/seo/sitemap