本文最后更新于 2019-03-05【1849 天前】,文中所描述的信息可能已发生改变,请谨慎使用。如有问题或建议,欢迎在文章底部留言参与讨论!

站点地图 Sitemap 是网站 SEO 的一种重要的方式,站点地图的存在,可方便的告知搜索引擎网站有哪些可供抓取的网页,有利于搜索引擎蜘蛛对网站信息的结构化收录。 Typecho 这个简洁的博客程序,默认并没有提供 sitemap,不过我们可以通过插件或者不使用插件可以生成 sitemap。

Typecho,念念不忘,必有回响

Typecho Sitemap 插件

网上 Typecho 的 Sitemap 插件还是挺多的,一般使用较多的是 Hanny 版本以及 八云酱的版本,二者基本上没什么太大的差别,
无非就是 下载 - 上传 - 设置启用 三步走。

下载插件

从作者博客网站或者 Github 上下载插件,不建议从其他第三方下载。

上传

利用 FTP 或者其他方式将下载好的插件上传到 Typecho 安装路径下的 /usr/plugins 下面,一般还需要将文件夹的名字修改下,注意大小写,可能有些时候不必修改,但仍然建议修改。

设置并启用

登陆 Typecho 后台,在插件管理处,启用相应插件即可。可能有些其他版本的 Sitemap 插件还会员设置 sitemap 文件存放路径,是否建立索引,以及设置文章的优先级与更新频率等问题。博主的建议是,文件存放路径设置为 / 即网站的根目录,这样 sitemap 的访问路径即是一般认为的 https://domain.com/sitemap.xml

无插件实现 Typecho 站点地图

对于博主这种懒得搭理,文章不多,更新频率也不高的,还是搭建在虚拟主机上的,对于插件的使用是能少一个就少一个的,就在 Yephy 的网站上找到了不用插件即生成站点地图的方法。

其实原来很简单,设置一个页面,自动替换和调用 Typecho 的文章、页面等信息。使用方法更简单,下载上传文件,创建页面即可。

下载上传文件

下载 sitemap.php 文件,复制下面的代码,保存为 sitemap.php,page-sitemap.php 等任何你认为文件名合适的 php w文件。

[collapse title="原作版本" status="false"]

<?php
/**
*Template Page of Sitemap
*
* @package custom
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
  <meta http-equiv="Content-Type" content="text/html; charset=<?php $this->options->charset(); ?>" />
  <title>站点地图 - <?php $this->options->title() ?></title>
  <meta name="keywords" content="站点地图,<?php $this->options->title() ?>" />
  <meta name="copyright" content="<?php $this->options->title() ?>" />
  <link rel="canonical" href="<?php $this->permalink() ?>" />
  <style type="text/css">
    body {font-family: Microsoft Yahei,Verdana;font-size:13px;margin:0 auto;color: #000000;background: #ffffff;width: 990px;margin: 0 auto}
    a:link,a:visited {color:#000;text-decoration:none;}
    a:hover {color:#08d;text-decoration:none;}
    h1,h2,h3,h4,h5,h6 {font-weight:normal;}
    img {border:0;}
    li {margin-top: 8px;}
    .page {padding: 4px; border-top: 1px #EEEEEE solid}
    .author {background-color:#EEEEFF; padding: 6px; border-top: 1px #ddddee solid}
    #nav, #content, #footer {padding: 8px; border: 1px solid #EEEEEE; clear: both; width: 95%; margin: auto; margin-top: 10px;}
  </style>
</head>
<body vlink="#333333" link="#333333">
  <h2 style="text-align: center; margin-top: 20px"><?php $this->options->title() ?>'s SiteMap </h2>
  <center></center>
  <div id="nav"><a href="<?php $this->options ->siteUrl(); ?>"><strong><?php $this->options->title() ?></strong></a> &raquo; <a href="<?php $this->permalink() ?>">站点地图</a></div>
  <div id="content">
    <h3>最新文章</h3>
    <ul>
      <?php
      $stat = Typecho_Widget::widget('Widget_Stat');
      $this->widget('Widget_Contents_Post_Recent', 'pageSize='.$stat->publishedPostsNum)->to($archives);
      $year=0; $mon=0; $i=0; $j=0;
      while($archives->next()){
          $year_tmp = date('Y',$archives->created);
          $mon_tmp = date('m',$archives->created);
          $y=$year; $m=$mon;
          if ($year > $year_tmp || $mon > $mon_tmp) {
              $output .= '</ul>';
          }
          $output .= '<l><a href="'.$archives->permalink .'">'. $archives->title .'</a></l>';
      }
      $output .= '</ul>';
      echo $output;
      ?>
    </ul>
  </div>
  <div id="content">
    <li class="categories">分类目录
      <ul><?php $this->widget('Widget_Metas_Category_List')
               ->parse('<l><a href="{permalink}">{name}</a> ({count})</l>'); ?>
      </ul>
    </l>
  </div>
  <div id="content">
    <li class="categories">单页面</l>
    <ul class="clearfix" id="nav_menu">
      <l><a href="<?php $this->options->siteUrl(); ?>">Home</a></l>
      <?php $this->widget('Widget_Contents_Page_List')
                 ->parse('<l><a href="{permalink}">{title}</a></l>'); ?>
    </ul>
  </div>
  <div id="footer">查看博客首页: <strong><a href="<?php $this->options ->siteUrl(); ?>"><?php $this->options->title() ?></a></strong></div><br />
  <center>
    <div style="text-algin: center; font-size: 11px"><br /> &copy; <?php echo date('Y'); ?> <strong><a href="https://www.yephy.com" target="_blank">YephyBlog</a></strong> 版权所有<br /><br /><br />
    </div>
  </center>
</body>
</html>

[/collapse]

原作的版本是有一点小问题的,至少是在 php7 + Typecho 1.1 环境下,会提示异常 Notice: Undefined variable: output ,我查了些资料,进行了小修正,并将 标签 (tag) 信息也作为 sitemap 输出了。
[collapse title="Quanyin 修改版" status="false"]

<?php
/**
* Sitemap
*
* @package custom
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml/">
<head profile="https://gmpg.org/xfn/11">
  <meta http-equiv="Content-Type" content="text/html; charset=<?php $this->options->charset(); ?>" />
  <title>站点地图 - <?php $this->options->title() ?></title>
  <meta name="keywords" content="站点地图,sitemap,<?php $this->options->title() ?>" />
  <meta name="copyright" content="<?php $this->options->title() ?>" />
  <link rel="canonical" href="<?php $this->permalink() ?>" />
  <style type="text/css">
    body {font-family: Microsoft Yahei,Verdana;font-size:13px;margin:0 auto;color: #000000;background: #ffffff;width: 990px;margin: 0 auto}
    a:link,a:visited {color:#000;text-decoration:none;}
    a:hover {color:#08d;text-decoration:none;}
    h1,h2,h3,h4,h5,h6 {font-weight:normal;}
    img {border:0;}
    li {margin-top: 8px;}
    .page {padding: 4px; border-top: 1px #EEEEEE solid}
    .author {background-color:#EEEEFF; padding: 6px; border-top: 1px #ddddee solid}
    #nav, #content, #footer {padding: 8px; border: 1px solid #EEEEEE; clear: both; width: 95%; margin: auto; margin-top: 10px;}
  </style>
</head>
<body vlink="#333333" link="#333333">
  <h2 style="text-align: center; margin-top: 20px"><?php $this->options->title() ?>'s SiteMap </h2>
  <center></center>
  <div id="nav"><a href="<?php $this->options ->siteUrl(); ?>"><strong><?php $this->options->title() ?></strong></a> &raquo; <a href="<?php $this->permalink() ?>">站点地图</a></div>
  <div id="content">
    <h3>最新文章</h3>
    <ul>
    <?php
      $output = '';
      $stat = Typecho_Widget::widget('Widget_Stat');
      $this->widget('Widget_Contents_Post_Recent', 'pageSize='.$stat->publishedPostsNum)->to($archives);
      $year=0; $mon=0; $i=0; $j=0;
      while($archives->next()){
          $year_tmp = date('Y',$archives->created);
          $mon_tmp = date('m',$archives->created);
          $y=$year; $m=$mon;
          if ($year > $year_tmp || $mon > $mon_tmp) {
              $output .= '</ul>';
          }
          $output .= '<l><a href="'.$archives->permalink .'">'. $archives->title .'</a></l>';
      }
      $output .= '</ul>';
      echo $output;
      ?>
    </ul>
  </div>
  <div id="content">
    <li class="categories">独立页面</l>
    <ul class="clearfix" id="nav_menu">
      <l><a href="<?php $this->options->siteUrl(); ?>">Home</a></l>
      <?php $this->widget('Widget_Contents_Page_List')
                 ->parse('<l><a href="{permalink}">{title}</a></l>'); ?>
    </ul>
  </div>
  <div id="content">
    <li class="categories">分类目录
      <ul><?php $this->widget('Widget_Metas_Category_List')
               ->parse('<l><a href="{permalink}">{name}</a> ({count})</l>'); ?>
      </ul>
    </l>
  </div>
  <div id="content">
    <li class="categories">标签云
      <ul><?php $this->widget('Widget_Metas_Tag_Cloud')
               ->parse('<l><a href="{permalink}">{name}</a> ({count})</l>'); ?>
      </ul>
    </l>
  </div>
  <div id="footer">查看博客首页: <strong><a href="<?php $this->options ->siteUrl(); ?>"><?php $this->options->title() ?></a></strong></div><br />
  <center>
    <div style="text-algin: center; font-size: 11px"><br /> &copy; <?php echo date('Y'); ?> <strong><a href="https://www.imtqy.com" target="_blank">Quanyin</a></strong> 版权所有<br /><br /><br />
    </div>
  </center>
</body>
</html>

[/collapse]

将上述代码保存之后,与之前插件上传目录不同的是,这个文件是上传到 Typecho 主题文件里,即 /usr/themes/yourtheme/

创建站点地图

在 Typecho 选择增加独立页面,其中,slug 设置为 sitemap,同时模板选择 sitemap,不需要任何内容,直接发布文章即可,如下图所示,图片来自于原作:
创建站点地图

访问 http://yourdomain.com/sitemap.html,即可看到生成的站点地图,本站的站点地图:https://www.imtqy.com/sitemap.html

一个遗留的问题

上述创建的 sitemap 是 html 页面,你也可以自行修改上述文件生成标准的 sitemap.xml 文件。这里,提供一个 rewrite 规则,将对 sitemap.xml 跳转到 sitemap.html.

Apache

将下面的代码写进到网站目录下的 .htaccess 即可

RewriteRule ^sitemap\.xml$ sitemap.html [L,R]

Nginx 的跳转规则

location /sitemap {
  rewrite ^/sitemap\.xml$ /sitemap.html redirect;
}
参考链接
文章目录


推荐使用:阿里云 云翼计划学生优惠、ECS、轻量应用等产品与服务【 点击注册

本文作者:Quanyin Tang

本文链接:Typecho 生成站点地图 Sitemap 的两种方式 - https://www.imtqy.com/typecho-sitemap.html

版权声明:如无特别声明,本文即为原创文章,仅代表个人观点,版权归 Quanyin 所有,未经允许禁止转载,经授权转载请注明出处!