最近接了个要做搜索引擎优化的站,当然还是用Wordpress做的。 因为以前有接触过红动中国设计网的老股东,从他哪里了解过一点优化这块。 涉及原因还挺多的,包括程序代码结构,所以这次就给找了个代码结构不错的Wordpress主题。
百度是最坑站长的一个搜索了,但是他家在国内市场率太高,也没办法。 这类网上的教程一大堆,这里只是慢慢总结下。
先说下基础吧:
一:伪静态
这个Wordpress后台固定链接这里根据自己喜好修改就行了。
然后把伪静态规则加到网站对应配置nginx文件就行。
location / {
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* ^/wp-content/uploads/.*.php$ {
deny all;
}
二:robots.txt文件
这个现在Wordpress会自动生成,直接用就行了。
可以在里面加上站点地图链接就行。
User-agent: *
Disallow: /wp-admin/
Disallow: /wp-content/
Disallow: /wp-includes/
Disallow: //comment-page-
Disallow: /?replytocom=
Disallow: /category//page/ Disallow: /tag//page/
Disallow: //trackback Disallow: /feed Disallow: //feed
Disallow: /comments/feed
Disallow: /?s=*
Disallow: //?s=\
Disallow: /attachment/
Sitemap: 网址/sitemap.xml
Sitemap: 网址/sitemap.html
三:sitemap.xml站点地图
这个也是可以给搜索引擎用的。
1:先准备个PHP文件(代码来自网络)
<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 100000;
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'); ?> -->
<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
$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); ?>+00:00</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>
2:把这个文件随便取名,比如sitemap.php,放到网站的目录下。
3:修改网站的nginx配置文件,加入伪静态。
rewrite ^/sitemap.xml$ /sitemap.php;
4:每天凌晨定时生成xml文件,等搜索蜘蛛来爬的时候也方便直接抓取,不用等一会。
用Linux的定时任务:
crontab -e
01 01 * * * wget -O /网站目录/sitemap.xml --no-check-certificate 网站链接/sitemap.php >/dev/null 2>&1
根据系统版本用命令重启下crond.service
5:百度、谷歌、360等站长工具里面有个链接提交,把这个sitemap.xml提交下就行。