使用 PHP 创建 XML 站点地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2747801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an XML sitemap with PHP
提问by williamg
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.xmlI think the main problem is that its not recognizing the PHP code. Here's the full source:
我正在尝试创建一个会自动更新的站点地图。我已经做了一些与我的 RSS 提要类似的事情,但是这个站点地图拒绝工作。您可以在http://designdeluge.com/sitemap.xml实时查看我认为主要问题是它无法识别 PHP 代码。这是完整的来源:
<?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>
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!
问题是动态网址(例如从数据库中提取的网址)没有生成,站点地图也不会验证。谢谢!
EDIT: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 文件。上面的代码正在被使用。现在,屏幕或源中没有任何显示。我想我犯了一个语法错误,但我找不到任何东西。任何和所有的帮助表示赞赏!
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!
编辑 2:好的,我把它整理好了。显然,我不得不用 PHP 回应 xml 声明。最后的代码贴在上面。谢谢你的帮助!
回答by Pascal MARTIN
If you take a look at the sitemap.xmlthat's generated (using view source, in your browser, for example), you'll see this :
如果您查看sitemap.xml生成的(例如,在浏览器中使用查看源代码),您将看到:
<?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:/
...
The <?php, present in that output, shows that PHP code is not interpreted.
的<?php,存在于该输出,显示,PHP代码不被解释。
This is probably because your webserver doesn't recognize .xmlas an extension of files that should contain PHP code.
这可能是因为您的网络服务器无法识别.xml为应包含 PHP 代码的文件扩展名。
At least two possible solutions :
至少有两种可能的解决方案:
- 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.phpfor example, so it's interpreted by your server.
- 重新配置您的服务器,以便 XML 文件通过 PHP 解释器(可能不是一个好主意:这可能会导致现有文件出现问题!)
sitemap.php例如,将站点地图的扩展名更改为由您的服务器解释。
I would add another solution :
我会添加另一个解决方案:
- Have a
sitemap.phpfile, that contains the code - And use a RewriteRuleso the
sitemap.xmlURL actually points to thesitemap.phpfile
- 有一个
sitemap.php包含代码的文件 - 并使用 RewriteRule使
sitemap.xmlURL 实际上指向sitemap.php文件
With that, you'll have the sitemap.xmlURL, which is nice (required ? ), but as the code will be in sitemap.php, it'll get interpreted.
有了它,您将拥有sitemap.xmlURL,这很好(必需?),但由于代码将在 中sitemap.php,它将被解释。
See Apache's mod_rewrite.
回答by Chris9
I've used William's code (thanks) and with a few small modifications it worked for me.
我已经使用了威廉的代码(谢谢),并进行了一些小的修改,它对我有用。
I think the line:
我认为这一行:
header("Content-type: text/xml");
should be the second line after the top <?php
应该是顶部之后的第二行 <?php
Incidentally, just a small point to anyone else that copies it, but there is a single space character before the <?phpin the first line - if you inadvertantly copy it as I did, you will spend a bit of time trying to figure out why the code won't work for you!
顺便说一句,对于复制它的其他人来说只是一个小点,但是<?php在第一行的之前有一个空格字符- 如果你像我一样不小心复制它,你会花一些时间试图弄清楚为什么代码不会为你工作!
I had to tweak the MySql select statement a little bit too.
我也不得不稍微调整一下 MySql select 语句。
Finally, in the output, I have used a variable $domain so that this piece of code can be used as a template without the need to think about it (provided you use the same table name each time). The variabe is added to the connectdb.php file which is included to connect to the database.
最后,在输出中,我使用了一个变量 $domain 以便这段代码可以用作模板而无需考虑(前提是您每次都使用相同的表名)。变量被添加到 connectdb.php 文件中,该文件包含在连接数据库中。
Here is my working version of the William's code:
这是我的威廉代码的工作版本:
<?php
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
include 'includes/connectdb.php';
?>
<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://www.DOMAIN.co.uk/</loc>
<priority>1.00</priority>
</url>
<?php
$sql = "SELECT * FROM pages WHERE onshow = 1 ORDER BY id ASC";
$result = mysql_query($sql,$conn);
while($row = mysql_fetch_array($result))
{
$filename = stripslashes($row['filename']);
?>
<url>
<loc>http://www.<?php echo "$domain"; ?>/<?php echo "$filename" ?></loc>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
<?php } ?>
</urlset>
回答by Jo?o Pimentel Ferreira
The best solution is to add to your apache .htaccessfile the following line after RewriteEngine On
最好的解决方案是将以.htaccess下行添加到您的 apache文件中RewriteEngine On
RewriteRule ^sitemap\.xml$ sitemap.php [L]
and then simply having a file sitemap.phpin your root folder that would be normally accessible via http://yoursite.com/sitemap.xml, the default URL where all search engines will firstly search.
然后只需sitemap.php在您的根文件夹中放置一个文件,该文件通常可以通过http://yoursite.com/sitemap.xml,所有搜索引擎将首先搜索的默认 URL访问。
The file sitemap.phpshall start with
该文件sitemap.php应以
<?php header('Content-type: application/xml; charset=utf-8') ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
it works :)
有用 :)

