前言:站点地图(sitemap.xml)的作用,相信站长们都有所了解,我就不献宝了。而免插件生成 sitemap.xml,网络上也早就有了纯代码生成的方法。
一直以来,张戈博客都是用 DX-SEO 这个很好用的中文 SEO 插件生成的 sitemap。今天整理电脑文件时,看到了以前收藏的生成 sitemap.xml 的 php 脚本,就随手打开看了看,发现这个代码只能生成主页和文章页的 sitemap。果断百度了一下,发现网上分享的都大同小异,只有首页和文章页。感觉有点缺憾,反正今天也是闲着,就动手改造了一番,让这个代码更加完善,可以同时生成首页、文章、单页面、分类和标签的 sitemap!
一、PHP 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
<?php
require(‘./wp-blog-header.php’);
header(“Content-type: text/xml”);
header(‘HTTP/1.1 200 OK’);
$posts_to_show=1000;
echo‘<?xml version=“1.0”encoding=“UTF-8”?>‘;
echo ‘<urlset xmlns=“http://www.sitemaps.org/schemas/sitemap/0.9”xmlns:mobile=“http://www.baidu.com/schemas/sitemap-mobile/1/”>‘
?>
<!—generated–on=<?phpechoget_lastpostdate(‘blog’);?>Diy By张戈博客(http://zhangge.net)–>
<url>
<loc><?phpechoget_home_url();?></loc>
<lastmod><?php$ltime=get_lastpostmodified(GMT);$ltime=gmdate(‘Y-m-dTH:i:s+00:00’,strtotime($ltime));echo$ltime;?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
/* 文章页面 */
$myposts=get_posts(“numberposts=”.$posts_to_show);
foreach($mypostsas$post){?>
<url>
<loc><?phpthe_permalink();?></loc>
<lastmod><?phpthe_time(‘c’)?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php}/* 文章循环结束 */?>
<?php
/* 单页面 */
$mypages=get_pages();
if(count($mypages)>0){
foreach($mypagesas$page){?>
<url>
<loc><?phpechoget_page_link($page->ID);?></loc>
<lastmod><?phpechostr_replace(” “,“T”,get_page($page->ID)->post_modified);?>+00:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
<?php}}/* 单页面循环结束 */?>
<?php
/* 博客分类 */
$terms=get_terms(‘category’,‘orderby=name&hide_empty=0’);
$count=count($terms);
if($count>0){
foreach($termsas$term){?>
<url>
<loc><?phpechoget_term_link($term,$term->slug);?></loc>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<?php}}/* 分类循环结束 */?>
<?php
/* 标签(可选) */
$tags=get_terms(“post_tag”);
foreach($tagsas$key=>$tag){
$link=get_term_link(intval($tag->term_id),“post_tag”);
if(is_wp_error($link))
returnfalse;
$tags[$key]->link=$link;
?>
<url>
<loc><?phpecho$link?></loc>
<changefreq>monthly</changefreq>
<priority>0.4</priority>
</url>
<?php }/* 标签循环结束 */?>
</urlset>
|
将以上代码保存为 sitemap.php,传到网站根目录。手动访问查看效果,如:http://zhangge.net/sitemap.php
二、伪静态
①、Nginx
编辑已存在的 Nginx 伪静态规则,新增如下规则后(平滑)重启 nginx 即可:
1
|
rewrite^/sitemap.xml$/sitemap.php last;
|
②、Apache
编辑网站根目录的 .htaccess ,加入如下规则:
1
|
RewriteRule^(sitemap).xml$$1.php
|
做好伪静态规则后,就可以直接访问 sitemap.xml 看看效果了,比如 http://zhangge.net/sitemap.xml
三、纯静态
此部分内容补充于:2016 年 10 月 24 日程序员节
看到很多朋友已经在问这个 sitemap 如何静态化,加快打开速度。毕竟每次重新生成绝对是一个耗能大户,而且还有可能被有心之人拿来作为攻击入口!
其实,张戈博客早就已经实现 sitemap.xml 静态化了,而且在后面的文章中也有提到=>【相关文章】
实现方法有多种,比如在 Nginx 的 fastcgi 缓存中取消 xml 文件的缓存屏蔽,或者使用张戈博客最早使用的 php 生成静态文件等。
在这里,我就分享一个自己一直在用的最简单的实现方法:Linux 定时任务+wget 定时生成 sitemap.xml
具体实现:将 sitemap.php 放到某个不为人知的目录,然后定时使用 wget 去请求这个文件,并将数据保存为 sitemap.xml 存放到网站根目录就可以了!比如:
1
2
|
#每天在网站根目录生成一个sitemap.xml diypath为sitemap.php的实际位置
01***wget–O/home/wwwroot/zhangge.net/sitemap.xmlhttp://zhangge.net/diypath/sitemap.php >/dev/null2>&1
|
2017-09-22 补充:如果是启用了 https 的站点,需要加入 –no-check-certificate 的选项,即:
1
2
|
#每天在网站根目录生成一个sitemap.xml diypath为sitemap.php的实际位置(针对https网站)
01***wget–O/home/wwwroot/zhangge.net/sitemap.xml—no–check–certificate https://zhangge.net/diypath/sitemap.php >/dev/null 2>&1
|
Ps:使用这个方法,注意 sitemap.php 里面的 require(‘./wp-blog-header.php’); 要改成 require(‘../wp-blog-header.php’); 也就是注意相对位置!
如果实在搞不清楚什么是相对路径,那么就用简单粗暴的方法:将网站根目录的 sitemap.php 重命名为一个只有自己知道的 php 文件,比如 xml.php,然后如下添加任务:
12 #每天在网站根目录生成一个sitemap.xml(xml.php为自己重命名的php文件名称)01***wget–O/home/wwwroot/zhangge.net/sitemap.xmlhttp://zhangge.net/xml.php >/dev/null2>&1
这样一来,就解决了 sitemap.xml 是动态数据问题了!
四、文章最后
①、确认无误之后,已开通 sitemap 权限的就可以前往百度站长平台提交了,没开通权限的可以发送申请邮件到百度站长平台管理员邮箱申请,并且将 sitemap.xml 使用 a 标签链接在网站底部即可。
②、代码使用很简单,可以根据需要增减内容,比如觉得标签不应该出现在 sitemap 里面的,可以将标签部分的 php 代码删除即可,但一定要注意不要误删除结尾的</urlset>标签。
③、今天,把分类、单页面及标签的 sitemap 都整出来了,那开放适配专用 sitemap 的 php 代码也就可以继续完善下了,回头有时间我会整理总结一篇关于 sitemap 及开放适配的终结篇,敬请期待!