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 访问请求
}
if ($http_user_agent !~* (iPhone|Android|android|iphone|ipod|ipad|windows\s*phone|blackberry|symbian|Baiduspider|Googlebot|bingbot|bot|crawl|spider)) {
return 403; # 拒绝所有 PC 访问请求
}
wordpress只禁止访问首页
set $PCindex "/path/to/pc/index.html";
location = / {
if ($http_user_agent !~* "bot|crawler|spider") {
return 403;
}
try_files $PCindex =404;
}
wp越来越大了