我应该使用什么文件扩展名来动态RSS

foo*_*how 2 php rss .htaccess

我目前正在开发一个动态RSS源,它将自动从MySQL数据库中提取文章.代码如下

<?php 

//Include the post retreival script
require_once '../phpScripts/rss_db_setup.php';

//Set the content type
header('Content-type: text/xml');

//Set up the RSS feed information
echo '<?xml version="1.0" encoding="ISO-8859-1"?>'.
 '<rss version="2.0">'.

 '<channel>'.
 '<title>Company Name</title>'.
 '<link>http://www.company.ca</link>'.
 '<description></description>'.
 '<category></category>';

//Retreive posts from the database
$rssData = new rssData();
echo $rssData->generateFeed($dbcon);

//Close the feed
echo '</channel></rss>';

?>
Run Code Online (Sandbox Code Playgroud)

我想知道这个文件是应该保存为.xml还是.php?我已将以下行添加到我的.htaccess文件中,但并不完全理解它是如何工作的

AddType application/x-httpd-php .xml
Run Code Online (Sandbox Code Playgroud)

这是一个正确的方法吗?或者我应该使用另一个htaccess函数,如modRewrite,还是使用CRON作业每天生成一个新的.xml?

Ger*_*ben 7

RSS不需要扩展名.它不关心网址是否/feed.php /feed.xml只是/feed/.它不像你自己的硬盘上的文件.HTTP发送内容类型标头以指定它是什么类型的文件.

然而,它(正式)需要正确的内容类型标题.在代码中指定text/xml,应该正式为application/rss + xml.

如果在服务器上的其他位置使用静态xml文件,则使用AddType可能会出现问题.PHP会在每个xml文件的开头部分阻塞,给出一个很好的错误消息,导致xml无效.