建立一个 HTML 的网站地图 (sitemap) 对于 SEO 和用户体验都有一定的优化,而单纯的建立一个 xml 的网站地图则是有利于 SEO 优化,之前小编曾经发布过一个 WordPress 的 xml sitemap 的插件:google sitemap generator,那么今天小编就来介绍下如何使用非插件的方法建立 HTML 以及 XML 格式的网站地图。

WordPresss 非插件建立 html 格式的网站地图的方法:

1 、首先下载小编这里为大家准备好了的 html 站点地图的模板文件:百度网盘

2 、将 html 站点地图模板文件上传至当前使用的 WordPress 主题的目录下。

3 、在后台新建一个内容为空标题为站点地图,模板为站点地图地图的页面,操作图如下:

选择模板

然后发布即可创建 html 格式的站点地图,好了,说完了如何不用插件制作 WordPress 的 html 格式的网站地图,接下来小编在教大家如何不使用插件创建 WordPress 的 xml 格式的网站地图。

xml 地图的创建方法就更简单了,新建一个名为 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: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">';
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->
  <url>
      <loc>https://www.weixiaoduo.com/</loc>
      <lastmod><?php echo get_lastpostdate('blog'); ?></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 } // end foreach ?>
</urlset>

将 xmlmap.php 文件传至网站根目录,然后根据环境写好 url 转发规则。

首先是 apache 下的规则:

RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ xmlmap.php

将以上代码加入到.htaccess 文件即可,接下来是 nginx 下规则:

rewrite ^/sitemap.xml$ /xmlmap.php;

至于微软的 iis,小编没用过,规则怎么写?自己慢慢琢磨吧。