使用php创建动态站点地图

时间:2016-08-29 06:07:46

标签: php xml sitemap

首先让我告诉你我正在学习php。我想为我的网站创建一个自动动态的站点地图。脚本(假设sitemap.php)将抓取我网站的所有链接,并将创建一个包含1000个网址的数组。一旦1000个URL限制到达,它将使另一个页面id与1000 urls数组。这样脚本输出就像这样:

<sitemapindex 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/siteindex.xsd">

<sitemap>
<loc>http://exmaple.com/sitemap.php?page=1</loc>
</sitemap>
<sitemap>
<loc>http://example.com/sitemap.php?page=2</loc>
</sitemap>
</sitemapindex>

第1页将有xml格式的1000个网址,第2页也将有,这将在网站更新时继续。我已经尝试了来自谷歌的github和其他搜索页面中的这么多代码。但还没找到我需要的东西。你能帮忙吗?

3 个答案:

答案 0 :(得分:9)

一个好的解决方案是访问您的根文件夹并在.htaccess

之后将以下行添加到您的apache RewriteEngine On文件中
RewriteRule ^sitemap\.xml$ sitemap.php [L]

然后在根文件夹中只有一个文件sitemap.php,因此通常可以通过http://yoursite.com/sitemap.xml访问,sitemap.php是所有搜索引擎首先搜索的默认网址。

文件<?php header('Content-type: application/xml; charset=utf-8') ?> <?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?> 必须以,以确保服务器发送正确的HTTP标头:

alert()

答案 1 :(得分:0)

这可能会对您有所帮助:Refer

这是CLI脚本。它会自动抓取您的网站,动态生成XML站点地图。

你需要知道下面的事情要在脚本上面运行:

  • PHP CLI
  • 卷曲
  • XML

如果您发现任何困难,请检查并告知我们。

感谢。

答案 2 :(得分:0)

一种解决方案是创建一个php脚本,该脚本生成带有所有网站URL的静态sitemap.xml。 之后,使用CRON作业定期运行php脚本(例如,每周一次)。这将自动更新站点地图。