wordpress 禁止PC首页访问 不影响蜘蛛抓取的方法

一、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 只是一些常见的,不一定是最新的。

@版权声明: 本站所有文章,如左上角无特殊说明,则归类于图图网原创,其它版权归原作者所有,如若本站内容侵犯了您的合法权益,可联系我们进行处理。

给TA支持
共{{data.count}}人
人已支持
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧