WordPress搜索引擎优化总结

2023-11-09 0 656

正文:

最近我接到了一个关于搜索引擎优化的项目,客户使用得是 WordPress 程序。

由于我也使用 WordPress 程序,懂得更深优化的技巧。

搜索引擎优化涉及的因素很多,包括程序代码结构,所以这次我选择了一个代码结构良好的 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 会自动为我们生成 robots.txt 文件,我们只需要直接使用即可。

我们可以在这个文件中添加站点地图的链接。

 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 文件提交即可。

转载请注明:汇站网 » WordPress 搜索引擎优化总结

收藏 (0)

微信支付 微信扫一扫

支付宝支付 支付宝扫一扫

点赞 (0)

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务