MENU

纯代码为网站添加sitemap网站地图优化SEO

网站地图,又称站点地图,它就是一个页面,上面放置了网站上需要搜索引擎抓取的所有页面的链接(注:不是所有页面)。搜索引擎蜘蛛非常喜欢网站地图网站地图是一个网站所有链接的容器。很多网站的连接层次比较深,蜘蛛很难抓取到,网站地图可以方便搜索引擎蜘蛛抓取网站页面,通过抓取网站页面,清晰了解网站的架构,网站地图一般存放在根目录下并命名为sitemap,为搜索引擎蜘蛛指路,增加网站重要内容页面的收录。网站地图就是根据网站的结构、框架、内容,生成的导航网页文件。大多数人都知道网站地图对于提高用户体验有好处:它们为网站访问者指明方向,并帮助迷失的访问者找到他们想看的页面。对于SEO,网站地图的好处就更多了。

WordPress生成网站地图的插件也有很多,大家可以自行百度,这里主要是讲利用代码生成。

        • *

1.将以下代码将保存为 sitemap.php,传到网站根目录。

  1. <?php
  2. require('./wp-blog-header.php');
  3. header("Content-type: text/xml");
  4. header('HTTP/1.1 200 OK');
  5. $posts_to_show = 1000;
  6. echo '<?xml version="1.0" encoding="UTF-8"?>';
  7. echo '<urlset xmlns="https://www.<;a href="https://www.sunweihu.com/tag/sitemap"; title="查看更多关于sitemap的文章" target="_blank">sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="https://www.baidu.com/schemas/sitemap-mobile/1/">'
  8. ?>
  9. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>墨涩网(https://www.sunweihu.com)-->;
  10. <url>
  11. <loc><?php echo get_home_url(); ?></loc>
  12. <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-dTH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
  13. <changefreq>daily</changefreq>
  14. <priority>1.0</priority>
  15. </url>
  16. <?php
  17. / 文章页面 /
  18. $myposts = get_posts( "numberposts=" . $posts_to_show );
  19. foreach( $myposts as $post ) { ?>
  20. <url>
  21. <loc><?php the_permalink(); ?></loc>
  22. <lastmod><?php the_time('c') ?></lastmod>
  23. <changefreq>monthly</changefreq>
  24. <priority>0.6</priority>
  25. </url>
  26. <?php } / 文章循环结束 / ?>
  27. <?php
  28. / 单页面 /
  29. $mypages = get_pages();
  30. if(count($mypages) > 0) {
  31. foreach($mypages as $page) { ?>
  32. <url>
  33. <loc><?php echo get_page_link($page->ID); ?></loc>
  34. <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
  35. <changefreq>weekly</changefreq>
  36. <priority>0.6</priority>
  37. </url>
  38. <?php }} / 单页面循环结束 / ?>
  39. <?php
  40. / 博客分类 /
  41. $terms = get_terms('category', 'orderby=name&hide_empty=0' );
  42. $count = count($terms);
  43. if($count > 0){
  44. foreach ($terms as $term) { ?>
  45. <url>
  46. <loc><?php echo get_term_link($term, $term->slug); ?></loc>
  47. <changefreq>weekly</changefreq>
  48. <priority>0.8</priority>
  49. </url>
  50. <?php }} / 分类循环结束 /?>
  51. <?php
  52. / 标签(可选) /
  53. $tags = get_terms("post_tag");
  54. foreach ( $tags as $key => $tag ) {
  55. $link = get_term_link( intval($tag->term_id), "post_tag" );
  56. if ( is_wp_error( $link ) )
  57. return false;
  58. $tags[ $key ]->link = $link;
  59. ?>
  60. <url>
  61. <loc><?php echo $link ?></loc>
  62. <changefreq>monthly</changefreq>
  63. <priority>0.4</priority>
  64. </url>
  65. <?php } / 标签循环结束 / ?>
  66. </urlset>

如果你是小白或者懒得新建php文件的话那就下载下面这个php文件后解压上传到网站根目录吧

下载地址:https://www.lanzous.com/i2v6qyj

2.编辑 Nginx 伪静态规则

新增规则:

  1. rewrite ^/sitemap.xml$ /sitemap.php last;

3.编辑网站根目录的.htaccess

(如果你的网站根目录没有.htaccess则新建为txt文档)加入如下规则:

  1. RewriteRule ^(sit emap).xml$ $1.php

4.测试网站地图

先访问:https://你的网址/sitemap.php

再访问:https://你的网址/sitemap.xml

如果能看到你网站的最新数据那就说明成功了。

5.给搜索引擎提交网站地图

以百度站长为例

访问[](https://www.sunweihu.com/go.html?url=https://ziyuan.baidu.com/linksubmit/index)https://ziyuan.baidu.com/linksubmit/index

依次打开"链接提交"---"自动提交"---"sitemap"输入你的网站地图地址后提交即可。

0:00