熟悉SEO的朋友都知道,對于網(wǎng)站外部鏈接失效的情況如果鏈接帶有rel="nofollow"屬性可以避免不必要的損失。本文就以實(shí)例形式演示了PHP實(shí)現(xiàn)對站點(diǎn)內(nèi)容外部鏈接的過濾方法。具體如下:
問題描述:原來站內(nèi)很多文章都是摘錄的外部文章,文章里很多鏈接要么是時間久了失效了,要么就是一些測試的網(wǎng)址,如:http://localhost/ 之類的,鏈接多了的話,就形成站內(nèi)很多死鏈接,這對SEO優(yōu)化是很不利的。
解決方法:需要對站點(diǎn)內(nèi)的內(nèi)容進(jìn)行過濾,將不是內(nèi)部鏈接的鏈接加上 rel="nofollow"屬性。
本文借鑒了wordpress的過濾外部鏈接的函數(shù),將其改一下即可使用。
function content_nofollow($content,$domain){
preg_match_all('/href="(.*?)"/',$content,$matches);
if($matches){
foreach($matches[1] as $val){
if( strpos($val,$domain)===false )
$content=str_replace('href="'.$val.'"', 'href="'.$val.'" rel="external nofollow" ',$content);
}
}
preg_match_all('/src="(.*?)"/',$content,$matches);
if($matches){
foreach($matches[1] as $val){
if( strpos($val,$domain)===false )
$content=str_replace('src="'.$val.'"', 'src="'.$val.'" rel="external nofollow" ',$content);
}
}
return $content; }
新聞熱點(diǎn)
疑難解答
圖片精選