20

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

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

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

4

4 回答 4

60

你可以使用我的代码:

控制器/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);
    }
}

意见/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

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

对不起,如果代码中有一些错误,我是专门为你做的。如果有错误,您可以通过了解原理来轻松修复它们。

于 2012-06-25T12:55:01.543 回答
15

必须设置标题:

<?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>
于 2012-11-16T14:25:59.980 回答
6

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

Sitemap: http://www.yoursite.com/seo/sitemap
于 2015-05-24T09:21:36.840 回答
1

I've written a CodeIgniter model which enabled you to call functions from a sitemap controller and spit out the XML when you're all done feeding the sitemap.

Feel free to have a look and reuse the CodeIgniter model:

https://github.com/alphabase/CodeIgniter-Sitemap-Generator

于 2016-04-07T20:42:11.137 回答