我们知道网站地图能够很好的将整站的结果展现给搜索引擎,更利于搜索引擎抓取网页的内容,之前博客一直用得
Baidu Sitemap Generator插件实现的。如今在网上找到了一个可以不用插件的方法,现在分享给大家
生成html地图
生成html地图的方法比较简单,就是通过一个文件模板生成一个地图单页面就可以了
1、下载sitemap.php文件,通过ftp上传到主题文件夹里。
下载地址:http://pan.baidu.com/s/1c1GlCHi
2、新建一个单页面,注意这个单页面的地址就是网站地图的地址
例如:我的网站地图地址是:https://www.4xseo.com/sitemap/

地图生成以后,可以放在底部或顶部,方便用户浏览,也可以在robots.txt中添加
Sitemap: https://www.4xseo.com/sitemap
生成xml地图
html地图其实更多是给用户看的,而xml就完全是给搜索引擎“看”的了,生成xml的步骤稍微复杂些,需要设置url转发
具体步骤如下
1、下载xmlmap.php文件,通过ftp上传到网站根目录下。
下载地址:http://pan.baidu.com/s/1mh7NMn2
2、创建一个sitemap.xml文件,通过ftp上传到网站根目录下。
3、设置url转发规则文件:
如果你的主机是Apache,在.htaccess添加以下重写规则:
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ xmlmap.php
注意要放在,# BEGIN WordPress和# END WordPress 之外,否则有可能被自动删除
如果你的主机是nginx,在.htaccess添加以下重写规则:
rewrite ^/sitemap.xml$ /xmlmap.php;
如果你的主机是IIS,在web.config添加以下重写规则:
<rule name="Rewrite to xmlmap.php" stopProcessing="true">
<match url="^sitemap.xml" />
<action type="Rewrite" url="xmlmap.php" />
</rule>
同样,可以在robots.txt中添加如下说明,告知搜索引擎
Sitemap: https://www.4xseo.com/sitemap.xml
20161216更新
以上xmlmap.php中只有文章的,下面这个代码更全,大家可以替换
<?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=<?php echo get_lastpostdate('blog'); ?> Diy By 余斗-->
<url>
<loc><?php echo get_home_url(); ?></loc>
<lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
<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>
<lastmod><?php the_time('c') ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } ?>
<?php
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) { ?>
<url>
<loc><?php echo get_page_link($page->ID); ?></loc>
<lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></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 ($terms as $term) { ?>
<url>
<loc><?php echo get_term_link($term, $term->slug); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<?php }} ?>
<?php
$tags = get_terms("post_tag");
foreach ( $tags as $key => $tag ) {
$link = get_term_link( intval($tag->term_id), "post_tag" );
if ( is_wp_error( $link ) )
return false;
$tags[ $key ]->link = $link;
?>
<url>
<loc><?php echo $link ?></loc>
<changefreq>monthly</changefreq>
<priority>0.4</priority>
</url>
<?php } ?>
</urlset>