熟悉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ù),將其改一下即可使用。
具體代碼如下:
//外部鏈接增加nofllow $content 內(nèi)容 $domain 當(dāng)前網(wǎng)站域名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ào)用的時候很好調(diào)用,如下是調(diào)用演示
$a['content'] = content_nofollow($a['content'],$domain); //將文章內(nèi)容里的鏈接增加nofllow屬性
注意!這里過濾的域名需要是不帶“/”的,如http://m.survivalescaperooms.com,這樣才可以很好的過濾。
相信本文所述的方法對大家的PHP項(xiàng)目開發(fā)有一定的借鑒價(jià)值。
新聞熱點(diǎn)
疑難解答