dongxiaolin2801 2014-11-05 03:47
浏览 64
已采纳

重定向到动态站点地图还是通过yiic生成?

I want to render dynamic sitemap for yii site. But i've failed to do it in url manager:

'urlManager'=>array(                
    'showScriptName'=>false, 
    'rules'=>array(                 
        'sitemap.xml/*'=>'/site/sitemapxml', 
        array('site/sitemapxml', 'pattern'=>'sitemap.xml', 'urlSuffix'=>''),
 ...

so i turned to .htaccess and i added a redirect like this:

Redirect 301 /app3/sitemap.xml http://tarex.ru/app3/index.php/?r=site/sitemapxml

Another option is to generate sitemap.xml as an file through an extension in terms of a yiic command. Would it be more time/recourse efficient?

  • 写回答

1条回答 默认 最新

  • doudang1890 2014-11-05 09:12
    关注

    First of all, your rule was incorrect, use this instead:

    'rules' => array(                 
        '/sitemap.xml' => '/site/sitemapxml', 
        ...
    )
    

    Now, regarding your question, it doesn't really matter that much, which way you will prefer, each of them has its own cons and pros.

    Controller action option

    Pros:

    • Easy to implement and sustain. It's just another action, you won't forget how sitemap is generated in your system.

    Cons:

    • Dynamic content, therefore, lower performance.

    Solution: use COutputCache filter for caching. Add this filter to your SiteController:

    public function filters()
    {
        return array(
            array(
                'COutputCache + sitemap',
                'duration' => 24 * 60 * 60 // cache for 24 hours
            ),
        );
    }
    

    Don't forget to configure cache component in your configuration file. Use CFileCache for that. Read the corresponding section of The Definitive Guide to Yii if you're not familiar with this topic.

    It's a pretty flexible solution, you can extend this example to make it reading caching duration from your configuration file if needed.

    yiic option

    Pros:

    • sitemap.xml is just a static file, served more efficiently by the web server

    Cons:

    • need to update it regularly and control this process
    • need to use something else except the framework, this requires some monitoring as well

    Solution: use a cron job to generate sitemap.xml.

    Although the final decision highly depends on particular details of the project, generally, I'd stick to the first approach.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。