使用PHP创建XML网站地图 [英] Creating an XML sitemap with PHP
问题描述
我正在尝试创建一个将自动更新的站点地图.我已经用RSS提要完成了类似的操作,但是此站点地图无法正常工作.您可以在 http://designdeluge.com/sitemap.xml 上实时查看它,我认为主要问题是它无法识别PHP代码.这是完整的来源:
I'm trying to create a sitemap that will automatically update. I've done something similiar with my RSS feed, but this sitemap refuses to work. You can view it live at http://designdeluge.com/sitemap.xml I think the main problem is that its not recognizing the PHP code. Here's the full source:
<?php
include 'includes/connection.php';
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
<url>
<loc>http://designdeluge.com/</loc>
<lastmod>2010-04-20</lastmod>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
<url>
<loc>http://designdeluge.com/about.php</loc>
<lastmod>2010-04-20</lastmod>
<changefreq>never</changefreq>
<priority>0.5</priority>
</url>
<?php
$entries = mysql_query("SELECT * FROM Entries");
while($row = mysql_fetch_assoc($entries)) {
$title = stripslashes($row['title']);
$date = date("Y-m-d", strtotime($row['timestamp']));
echo "
<url>
<loc>http://designdeluge.com/".$title."</loc>
<lastmod>".$date."</lastmod>
<changefreq>never</changefreq>
<priority>0.8</priority>
</url>";
} ?>
</urlset>
问题是动态URL(例如从数据库中提取的URL)没有生成,站点地图也无法验证.谢谢!
The problem is that the dynamic URL's (e.g. the ones pulled from the DB) aren't being generated and the sitemap won't validate. Thanks!
编辑:现在,我只是在尝试使代码本身正常工作.我将其设置为本地测试服务器上的PHP文件.上面的代码正在使用中.现在,在屏幕上或源代码中什么也没有显示.我以为我犯了语法错误,但找不到任何东西.任何帮助都将不胜感激!
Right now, I'm just trying to get the code itself working. I have it set up as a PHP file on my local testing server. The code above is being used. Right now, nothing displays nothing on screen or in the source. I'm thinking I made a syntax error, but I can't find anything. Any and all help is appreciated!
好的,我把它整理出来了.显然,我不得不用PHP回显xml声明.最终代码已发布在上方.感谢您的帮助!
EDIT 2: Ok, I got it sorted out guys. Apparently, I had to echo the xml declaration with PHP. The final code is posted above. Thanks for your help!
推荐答案
如果您查看生成的sitemap.xml
(例如,使用浏览器中的视图源),则您会看到的:
If you take a look at the sitemap.xml
that's generated (using view source, in your browser, for example), you'll see this :
<?php header('Content-type: text/xml'); ?>
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http:/
...
该输出中显示的<?php
表明 PHP代码未得到解释.
这可能是因为您的网络服务器无法将.xml
识别为应该包含PHP代码的文件的扩展名.
This is probably because your webserver doesn't recognize .xml
as an extension of files that should contain PHP code.
至少两个可能的解决方案:
At least two possible solutions :
- 重新配置服务器,以便XML文件通过PHP解释器(可能不是一个好主意:可能会导致现有文件出现问题!)
- 将站点地图的扩展名更改为
sitemap.php
,以便由服务器解释.
- Re-configure your server, so XML files go through the PHP interpreter (might not be such a good idea : that can cause problems with existing files ! )
- Change the extension of your sitemap, to
sitemap.php
for example, so it's interpreted by your server.
我会添加另一个解决方案:
I would add another solution :
- 具有一个
sitemap.php
文件,其中包含代码 - 然后使用RewriteRule ,因此
sitemap.xml
URL实际上指向sitemap.php
文件
- Have a
sitemap.php
file, that contains the code - And use a RewriteRule so the
sitemap.xml
URL actually points to thesitemap.php
file
有了它,您将拥有sitemap.xml
URL,这是不错的(必需?),但由于代码将在sitemap.php
中,因此将得到解释.
With that, you'll have the sitemap.xml
URL, which is nice (required ? ), but as the code will be in sitemap.php
, it'll get interpreted.
这篇关于使用PHP创建XML网站地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!