“nofollow” 標簽是Google、Yahoo和微軟公司前幾年一起提出的一個標簽,鏈接加上這個標簽后就不會被計算權值,搜索引擎支持nofollow屬性,在很大程度上抑制博客或論壇的垃圾留言。對站長來說是一件大好事。
nofollow的作用
nofollow主要有三個作用:
1.防止不可信的內容,最常見的是博客上的垃圾留言與評論中為了獲取外鏈的垃圾鏈接,為了防止頁面指向一些拉圾頁面和站點。
2.付費鏈接:為了防止付費鏈接影響Google的搜索結果排名,Google建議使用nofollow屬性。
3.引導爬蟲抓取有效的頁面:避免爬蟲抓取一些無意義的頁面,影響爬蟲抓取的效率。
有時候需要對文章進行自動處理nofollow,防止權重流失,對于做網站優化seo的來說很重要。
$host為不需要進行處理的站點域名,否則全部自動加上nofollow。
首先正則出a標簽和href,然后進行每一層級判斷處理。
如果是外鏈,而且沒有nofollow就自動加上。
- /**
- * 自動處理外鏈加上rel="nofollow"
- */
- function webOutUrlDispose($html)
- {
- $host = ['www.ythuaji.com.cn', $_SERVER['HTTP_HOST']];//站點host
- $pattern = '/<a href="([^"]*)"[^>]*>.*<\/a>/';
- preg_match_all($pattern, $html, $matches);
- for ($i = 0; $i < count($matches[0]); $i++) {
- if (!strstr($matches[1][$i], '://')) {
- continue;
- }
- $array = parse_url($matches[1][$i]);
- if (in_array($array['host'], $host)) {
- continue;
- }
- if (!strstr($matches[0][$i], 'rel=')) {
- $yuan = $matches[0][$i];
- $matches[0][$i] = str_replace('<a', '<a rel="nofollow"', $matches[0][$i]);
- $html = str_replace($yuan, $matches[0][$i], $html);
- }
- }
- return $html;
- }
dedecms使用的話,需要在/include/extend.func.php下新增如上方法
模板調用:
{dede:field.body function='webOutUrlDispose(@me)'/}
以上方法沒有測試,各位請自行測試,織夢CMS利用php正則讓文章的外鏈自動加nofollow