码上的那些事我爱编程程序员

Linux shell脚本自动生成sitemap.xml网站地图

2018-04-10  本文已影响105人  爱音乐的二狗子

1.在网站根目录(为了安全起见,建议放到一个不会轻易让人找到的路径下)下新建sitemap.php文件,内容如下:

<?php
require('./wp-blog-header.php');     //注意:此处为相对路径,根据实际情况填写   
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000; // 获取1000篇文章   
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
?>
<url>
<loc>http://www.cenliming.com/</loc>   
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
header("Content-type: text/xml");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } // end foreach ?>  
</urlset>

然后保存,退出。
2.然后在网站根目录创建sitemap.xml文件:

touch sitemap.xml

3.crontab 、wget 命令将网站地图内容输出到sitemap.xml中:

crontab -e 0 5 * * * wget -O /xxx/xxx/web/sitemap.xml http://www.cenliming.com/sitemap.php (注:/xxx/xxx/web/为网站根目录)

这样,就可以每天凌晨5点自动的生成网站地图数据了。

上一篇下一篇

猜你喜欢

热点阅读