等了很久都没有客户来咨询问题,在不经意的情况下,有一位站长朋友找过说不用插件实现xml地图,会不会是php空间的问题?代码弄了,就是不能成功,可能是规则的问题,对于这个问题首先小编说一下这个方法的原理:
首先是要编写一个php文件,当然里面是有代码的,具体的代码,一会小编会公布出来的,网上的那个代码貌似现在不能使用了,主要的就是这个php文件,在弄上去的时候一定要确定访问这个php文件是xml地图的形式并且不报任何的错误,才进行下一步,然后就是通过url重定向进行实现,当访问的sitemap.xml的时候调转到php文件去,原理就是这么简单,下面小编就告诉各位方法吧!
1、首先建立sitemap.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.9http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>--> <url> <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>
2、然后就是规则的问题了,就是找到.htaccess,如果没有那么就新建一个,但是您设置wordpress的静态问题的都是自动生成的,放入以下规则:
RewriteRule ^(sitemap)\.xml$ $1.php
就这样访问/sitemap.xml的时候就是xml地图了,这样就会给服务器减少压力,所以站长朋友可以试一试,需要注意的是sitemap.php的命名一定要是这样的,方可使用这个规则,不然不能实现的哟。
猜你喜欢