今天来分享一个禁止用用户访问,只允许蜘蛛访问的代码方法。
一、wordpress 禁止PC首页访问 不影响蜘蛛抓取的方法
function block_homepage_for_pc() {
if ( ! is_admin() && ! is_feed() ) {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if ( strpos( $user_agent, 'Googlebot' ) === false && strpos( $user_agent, 'Bingbot' ) === false && strpos( $user_agent, 'Slurp' ) === false && strpos( $user_agent, 'DuckDuckBot' ) === false ) {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
}
}
}add_action( 'template_redirect', 'block_homepage_for_pc' );
二、nginx 屏蔽网站首页访问 允许蜘蛛访问的方法
location = / {
if ($http_user_agent !~* (spider|bot|googlebot|bingbot|yandex|msnbot|slurp)) {
return 403; }
# other rules
}
三、NGINX 禁止PC访问 不影响蜘蛛抓取的方法
if ($http_user_agent !~* (android|iphone|ipod|ipad|windows\s*phone|blackberry|symbian|bot|crawl|spider)) {
return 403; # 拒绝所有 PC 访问请求
}
四、只禁止访问首页
set $PCindex "/path/to/pc/index.html";location = / {
if ($http_user_agent !~* "bot|crawler|spider") {
return 403;
}
try_files $PCindex =404;}
国内搜索引擎蜘蛛:
Baidu: Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)
360: Mozilla/5.0 (compatible; 360Spider/1.0; +http://www.360.cn/spider.html)
Sogou: Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)
Shenma: Mozilla/5.0 (compatible; ShenmaBot/1.0; http://www.shenma.com)
Yisou: Mozilla/5.0 (compatible; YisouSpider; +http://www.yisou.com/help/webmaster/spider.html)
Easou: EasouSpider; +http://www.easou.com/search/spider.html
国外搜索引擎蜘蛛:
Google: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
Bing: Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)
Yahoo: Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)
Yandex: Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)
Baidu: Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)
DuckDuckBot: DuckDuckBot/1.0; (+http://duckduckgo.com/duckduckbot.html)
需要注意的是,蜘蛛的 User-Agent 可能会随着搜索引擎的升级和更新而变化,因此这里列举的 User-Agent 只是一些常见的,不一定是最新的。
本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/382968.html
如有侵犯您的合法权益请发邮件951076433@qq.com联系删除