原phpcms的文章内自带了内链锚文本连接和javascript两种链接方式,为什么还需要修改做?
1、当前页面不能识别,也能链接到自己,SEO不友好,不行!
2、当前页面的链接,不同关键词链接到同一个url,SEO不友好,不行!
做为动手党,不行的地方自已动手,是一种享受,哈哈,有没有人跟我想法一样的?行动起来!GO!
首先,关闭掉content列的自带内链功能:
![f0f55d2b73ffacb001c590e5abfa0e3c.png](https://i-blog.csdnimg.cn/blog_migrate/def893f516cfc3d30eebabcaec8fad05.jpeg)
![713ebe0d3da245d8dea73caff39e325a.png](https://i-blog.csdnimg.cn/blog_migrate/5ec362f25d83eda8a6f3e0706db0030d.jpeg)
![77904d0b039c5ec9ac40431ca1b91c6c.png](https://i-blog.csdnimg.cn/blog_migrate/3faeee7b3fb7d1cd3132fb3cd96e03a5.jpeg)
![124f6751f1385ea49502ab5976f2a624.png](https://i-blog.csdnimg.cn/blog_migrate/7e9c777e53e6721db50ba017ae41a352.jpeg)
基本设置完成,在content模块下,修改index.php文件
在function show方法下搜索extarct函数,在其下面加入:
//自定义关联联连,请取消模型->内容里面自带的关联链接
$mem = new Memcache();
$mem->connect('127.0.0.1',11211);
$key = md5(APP_PATH.'__keylink__');
if(!($mem->get($key))){
$keylinkDB = pc_base::load_model('keylink_model');
$keylinkRs = $keylinkDB->select('');
$mem->add($key,serialize($keylinkRs),0,time()+3600);
}else{
$keylinkRs = unserialize($mem->get($key));
}
$content = preg_replace_callback('/(<a[^>]+>)(.*?)(</a>)/is',function($matches){return $matches[1].base64_encode($matches[2]).$matches[3];},$content);
$search = "/(alts*=s*|titles*=s*)["|'](.+?)["|']/is";
$content = preg_replace_callback($search, array($this, '_base64_encode'), $content);
$word = array();
$urls = array();
$replacement = array();
foreach($keylinkRs as $value){
if($value['url'] != $url){
if(!in_array($value['url']) && stristr($content,$value['word']) && !in_array($value['url'],$urls)){
$word[] = "/".$value['word']."/";
$replacement[] = '<a href="'.$value['url'].'" target="_blank" class="keylink">'.$value['word'].'</a>';
$urls[] = $value['url'];
}
}
}
if($word && $replacement){
$content = preg_replace($word,$replacement,$content,1);
}
$content = preg_replace_callback('/(<a[^>]+>)(.*?)(</a>)/is',function($matches){
if($matches[2] == base64_encode(base64_decode($matches[2]))){
return $matches[1].base64_decode($matches[2]).$matches[3];
}else{
return $matches[1].$matches[2].$matches[3];
}
},$content);
$content = preg_replace_callback($search, array($this, '_base64_decode'), $content);
在最后加入两个自定义函数:
function _base64_encode($matches) {
return $matches[1].""".base64_encode($matches[2]).""";
}
function _base64_decode($matches) {
return $matches[1].""".base64_decode($matches[2]).""";
}
这样,内容里面的内链就会被优化了,完全符合SEO标准的优化哦。这里注意,我使用了Memcache缓存,各位有需要的,也可以自已安装,windows下面安装稍微麻烦一点,如果是linux很容易,以CentOS来说,只需要执行:
yum -y install memcached php-pecl-memcache
然后启动即可:
/etc/rc.d/init.d/memcached start
----------------------------END------------------------