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?
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.
Pros:
Cons:
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.
Pros:
Cons:
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.