刚刚有个小伙伴提出让我实现这样的需求:
在Wordpress的日常应用中,我们需要针对搜索引擎正常显示,对正常用户显示为特殊的内容。
方法一
下面我们给php的代码解决方法
修改主题的 index.php
,将以下代码增加到第一行
<?php function is_spider() { echo $agent= strtolower($_SERVER['HTTP_USER_AGENT']); if (!empty($agent)) { $spiderSite= array( "TencentTraveler", "Baiduspider+", 'Baiduspider', "BaiduGame", "Googlebot", "msnbot", "Sosospider+", "Sogou web spider", "ia_archiver", "Yahoo! Slurp", "YoudaoBot", "Yahoo Slurp", "MSNBot", "Java (Often spam bot)", "BaiDuSpider", "Voila", "Yandex bot", "BSpider", "twiceler", "Sogou Spider", "Speedy Spider", "Google AdSense", "Heritrix", "Python-urllib", "Alexa (IA Archiver)", "Ask", "Exabot", "Custo", "OutfoxBot/YodaoBot", "yacy", "SurveyBot", "legs", "lwp-trivial", "Nutch", "StackRambler", "The web archive (IA Archiver)", "Perl tool", "MJ12bot", "Netcraft", "MSIECrawler", "WGet tools", "larbin", "Fish search", ); foreach($spiderSite as $val) { $str = strtolower($val); if (strpos($agent, $str) !== false) { return true; } } } else { return false; } } //根据useragent判断类型 if( is_spider() ){ //正常的页面代码 }else{ //正常用户看到的代码 }
方法二
使用以下js实现,添加js
document.ready=function ready(fn){if(document.addEventListener){document.addEventListener("DOMContentLoaded",function(){document.removeEventListener("DOMContentLoaded",arguments.callee,false);fn()},false)}else{if(document.attachEvent){document.attachEvent("onreadystatechange",function(){if(document.readyState=="complete"){document.detachEvent("onreadystatechange",arguments.callee);fn()}})}}};document.getElementsByTagName("html")[0].style.display="none";(function(){var itis=true;var titles=document.title;document.title="";document.ready(function(){try{if(itis){$("body").html("");$("body").load("/fake.txt",function(){document.title=$(".headertitle").text()})}else{document.title=titles}}catch(e){}document.getElementsByTagName("html")[0].style.display="block"})})();document.oncontextmenu=function(e){var e=e||window.event;e.returnValue=false;return false};window.onkeydown=function(e){if(e.ctrlKey&&e.keyCode==83){e.preventDefault();e.returnValue=false;return false}};
在根目录添加 /fake.txt
内容:
This is a test.
评论已关闭