如何使用 PHP 和 MySQL 创建站点地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4018919/
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
How to create a sitemap using PHP & MySQL
提问by HELP
I was wondering how can I create a sitemap using PHP & MySQL and is there any sitemap design examples you know of?
我想知道如何使用 PHP 和 MySQL 创建站点地图,是否有您知道的站点地图设计示例?
回答by Westy92
I use this on my site, it works well and you can point Google's webmaster tools to "this_file.php" and it works wonders!
我在我的网站上使用它,它运行良好,您可以将 Google 的网站管理员工具指向“this_file.php”,它产生了奇迹!
<?php header("Content-type: text/xml"); echo'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'; echo' <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; include '../include.php'; $sql = mysql_query("select blah from bleh"); while ($string = mysql_fetch_array($sql)){?> <url> <loc>http://www.domain.com/dir/<?echo $string['value'];?>/index.php</loc> <changefreq>weekly</changefreq> </url> <?php } ?> </urlset>
回答by Spudley
The question is very vague -- we would need to know a lot more about the rest of your site before you'll get a good answer.
这个问题非常含糊——在您得到一个好的答案之前,我们需要更多地了解您网站的其他部分。
It depends on the structure of your page and what you want included in the map.
这取决于您的页面结构以及您希望包含在地图中的内容。
If your site is relatively static, then you should save your sitemap as a static page, so it doesn't cause extra processing every time its loaded. But if your site gets updated frequently, then you may need to refresh the map often, so a dynamic one that refreshes every time it is loaded may be better.
如果您的站点相对静态,那么您应该将站点地图保存为静态页面,这样每次加载时都不会导致额外的处理。但是如果您的站点更新频繁,那么您可能需要经常刷新地图,因此每次加载时都会刷新的动态地图可能会更好。
If your PHP site has a CMS structure and all your pages are included in the CMS, then it should be relatively simple to run through the database and pull out links to all your pages (depending, of course, on the structure of your CMS).
如果您的 PHP 站点具有 CMS 结构并且您的所有页面都包含在 CMS 中,那么运行数据库并拉出指向所有页面的链接应该相对简单(当然,取决于您的 CMS 结构) .
On the other hand, if your site is not structured in a way that lets you do this, or you want to limit the pages that are shown on the map, then you may find it easier to run a spider across the site and store the results.
另一方面,如果您的站点的结构不允许您这样做,或者您想限制在地图上显示的页面,那么您可能会发现在整个站点上运行蜘蛛并存储结果。
There are plenty of existing site map programs already written. I googled php sitemap generator, and got a whole stack of results, of which quite a few look like they could be useful to you, even if only so you can download them to study their source code.
已经编写了大量现有的站点地图程序。我在php 站点地图生成器上搜索了php 站点地图生成器,得到了一大堆结果,其中有很多看起来对你有用,即使只是为了让你可以下载它们来研究它们的源代码。